C ++ data types are used to store values. They have names and data types. The data types of variables determine how to store the bits representing these values in the computer's memory, the maximum length allowed by the variable name varies with the compiler.
In C ++, you must declare the variable type before using the variable: int x1 = 1; int x = 1000; float y = 3.14; long z = 457000, the compiler can perform a type check to ensure that the program runs smoothly. Improper use of data types may cause compilation errors or warnings for Analysis and Correction before running.
Some data types are signed and unsigned. Signed) data types can contain positive and negative numbers, while unsigned data types can only contain positive numbers. Table 1.1 lists the data types, memory size, and possible value ranges in C ++.
Data Type byte value range:
- Char 1-128 to 126
-
- Unsigned char 1 0 to 255
-
- Short 2-32,768 to 32,767
-
- Unsigned short 2 0 to 65,535
-
- Long 4-2,147,483,648 to 2,147,483,648
-
- Unsigned long 4 0 to 4,294,967,295
-
- Int 4 is the same as long
-
- Unsigned int 4 and unsigned long
-
- Float 4 1.2E-38 to 3.4E381
-
- Double 8 2.2E-308 to 1.8E3082
-
- Bool 1 true or false
From the table above, we can see that int and long are the same. So why do C ++ need to differentiate these two data types? This is actually a legacy issue. In a 16-bit programming environment, int requires 2 bytes, and long requires 4 bytes. In a 32-bit programming environment, both types of data are stored in four bytes.
C ++ only generates 32-bit programs for data types, so int and long are the same. In C ++ Builder and BorLand C ++ 5.0, Bool is a real data type. Some C ++ compilers have the Bool keyword, so Bool is not a real data type.
Sometimes Bool is only a typedef, so that Bool is equivalent to int. Typedef actually creates an alias to enable the compiler to draw an equal sign between one symbol and another. The syntax of typedef is as follows: typedef int Bool; this tells the compiler that Bool is the alias of int. It indicates that only the double and float data types use floating point numbers with decimal points ).
Other data types only involve integer values. Although the integer data type can also specify a number with a decimal point, the fractional part is discarded and only the integer part is assigned to the integer variable. For example: int x = 3.75; the value of x is 3. Note that this integer is not rounded to the nearest decimal point.
By the way, floating point numbers are rarely used in most Windows programs. C ++ data types can be converted between different data types when necessary. Example: short result; long num1 = 200; long num2 = 200; result = num1 * num2; here I want to assign the product of two long integers to a short integer. Although this formula is mixed with two data types, C ++ can be used for conversion.
What will happen to the calculation result? The result will surprise you. It is 25536, which is the result of the bypass (wrop. As shown in Table 1.1, the maximum value of a short integer is 32767. What if I add 1 to the maximum value? The result is 32768. This is actually the same as the car's mileage from 99999 to 00000. To illustrate this, enter and run the program contained in listing 1.3.