The best C ++ data types Tutorial In 2024, In this tutorial you can learn Basic built-in types,typedef statement,Enumerated type,
When using a programming language for programming, the need to use a variety of variables to store a variety of information. Variable retains its stored value is the memory location. This means that when you create a variable, it will retain some space in memory.
You may want to store a variety of data types (such as character, wide character, integer, float, double floating point, boolean, etc.) information, the operating system will be based on the data type of the variable, and the decision to allocate memory What is stored in the reserved memory.
C ++ programmer provides a variety of built-in data types and user-defined data types. The following table lists the seven basic C ++ data types:
类型 | 关键字 |
---|---|
布尔型 | bool |
字符型 | char |
整型 | int |
浮点型 | float |
双浮点型 | double |
无类型 | void |
宽字符型 | wchar_t |
Some basic types can use one or more types of modifiers modified:
The following table shows the maximum and minimum memory, and the variables of this type in the various types of variables when the value stored in memory can store needs to occupy.
Types of | Place | range |
---|---|---|
char | 1 byte | -128 To 127 or 0-255 |
unsigned char | 1 byte | 0-255 |
signed char | 1 byte | -128 To 127 |
int | 4 bytes | -2147483648 To 2147483647 |
unsigned int | 4 bytes | 0-4294967295 |
signed int | 4 bytes | -2147483648 To 2147483647 |
short int | 2 bytes | -32768 To 32767 |
unsigned short int | Range | 0 to 65,535 |
signed short int | Range | -32768 To 32767 |
long int | 4 bytes | -2,147,483,647 To 2,147,483,647 |
signed long int | 4 bytes | The same long int |
unsigned long int | 4 bytes | 0 to 4,294,967,295 |
float | 4 bytes | +/- 3.4e +/- 38 (~ 7 digits) |
double | 8 bytes | +/- 1.7e +/- 308 (~ 15 digits) |
long double | 8 bytes | +/- 1.7e +/- 308 (~ 15 digits) |
wchar_t | 2 or 4 bytes | A wide character |
Can be learned from the table, the size of the variable will vary depending on the compiler and the computer being used.
The following examples will output on your computer the size of a variety of data types.
#include <iostream> using namespace std; int main() { cout << "Size of char : " << sizeof(char) << endl; cout << "Size of int : " << sizeof(int) << endl; cout << "Size of short int : " << sizeof(short int) << endl; cout << "Size of long int : " << sizeof(long int) << endl; cout << "Size of float : " << sizeof(float) << endl; cout << "Size of double : " << sizeof(double) << endl; cout << "Size of wchar_t : " << sizeof(wchar_t) << endl; return 0; }
This example usesendl, which will insert a line break after each line, << operator is used to pass multiple values to the screen.We also usesizeof () function to get the size of the various data types.
When the above code is compiled and executed, it produces the following results, the results will vary depending on the computer you are using:
Size of char : 1 Size of int : 4 Size of short int : 2 Size of long int : 4 Size of float : 4 Size of double : 8 Size of wchar_t : 4
You can usetypedef to a type already taken a new name.Here is typedef to define a new type of syntax:
typedef type newname;
For example, the following statement will tell the compiler, feet is another name for an int:
typedef int feet;
Now, the following statement is perfectly legal, it creates an integer variable distance:
feet distance;
Enum type (enumeration) is a C ++ in a derived data type, which is a collection of several enum constant defined by the user.
If a variable is only a few possible values can be defined as an enumeration (enumeration) type. Within the scope of the so-called "enumeration" refers to the value of the variable list them out, the values of variables can only be enumerated values.
Create enumeration, use the keywordenum.The general form of an enumeration type are:
enum enum-name { list of names } var-list;
Here, enum-name is the name of the enumerated type. Name list {list of names} are separated by commas.
For example, the following code defines a color enumeration variable c of type color. Finally, c is assigned the value "blue".
enum color { red, green, blue } c; c = blue;
By default, the value is 1, the third name value is 0, the second name is the first name is 2, and so on. However, you can also give the name given to a special value, just add an initial value. For example, in the followingenumeration, green value of 5.
enum color { red, green=5, blue };
Here, blue is 6, because by default, each name will be more than it is in front of a big name 1.