Chapter II Variables and variable types
1. C + + arithmetic type
BOOL Boolean minimum size undefined
Char character 8 bit
wchar_t wide character 16 bit
char16_t Unicode characters 16 bit
char32_t Unicode characters 32 bit
Short 16-bit
int integer 16-bit
Long 32-bit
Long length 64-bit
Float single-precision floating-point 6-bit valid digits
Double dual-precision floating-point 10-bit valid digits
Long double extended precision floating point Number 10 digits valid
How to choose a type
- When the value cannot be negative, the unsigned type is chosen. unsigned
- Performs an integer operation using int . In practical applications, short is often too small and long has the same size as int. If the value exceeds the representation range of int, select Long Long.
- Do not use char and bool in an arithmetic expression, only use them when storing characters or Boolean values. Because Char is signed on some machines, it is unsigned on other machines.
- Floating-point arithmetic uses double.
2. Type conversion
- when we a non- Boolean assign Boolean type
- When we assign a Boolean value to a non- Boolean type, the initial value is false and the result is 0, and the initial value is true and the result is 1.
- when a floating-point number is assigned to an integer type, approximate processing occurs. The result value retains only the portion of the float before the decimal point. will not be rounded. When
- assigns integers to floating-point types, the fractional part is recorded as 0. If an integer occupies more space than a floating-point type, the accuracy can be lost.
Although we do not intentionally assign a negative value to an unsigned object, it is possible to write such a code. For example, when an arithmetic expression has both an unsigned number and an int value, that int value is converted to an unsigned number, converting int to an unsigned process and assigning an int directly to an unsigned variable. For example unsigned u =10;int i= -42;std::cout<<i+i<<std::endl;std::cout<<u+i<<std::endl; int is 32 bits, output 4294967264 The first expression, two numbers are added to get the expected value, and in the second expression, the integer-42 is converted to an unsigned number before adding. Converting negative numbers to unsigned numbers is equivalent to assigning a negative value directly to an unsigned number, and the result equals this negative number plus the modulo of the unsigned numbers. Escape sequence line break \ n Portrait tab \v backslash \ return character \ r transverse tab \ t backspace \b question mark \? Paper feed character \f Alarm \a double quotation mark \ "single quote \ '
2017.11.10 Reread C + + Primer