Variable and base type number of bytes in base type on different platforms
Type |
16-bit Platform |
32-bit Platform |
64-bit Platform |
Char |
1 |
1 |
1 |
Short |
2 |
2 |
2 |
Int |
2 |
4 |
4 |
Long |
4 |
4 |
8 |
Long Long |
/ |
8 |
8 |
Pointer |
2 |
4 |
8 |
Float |
4 |
4 |
4 |
Double |
8 |
8 |
8 |
Minimum size of type
Type |
Meaning |
Minimum size |
bool |
Boolean type |
Not defined |
Char |
Character |
8 Guests |
wchar_t |
Wide character |
16 Guests |
char16_t |
Unicode characters |
16 Guests |
char32_t |
Unicode characters |
32 Guests |
Short |
Short-integer |
16 Guests |
Int |
Integral type |
16 Guests |
Long |
Long integer type |
32 |
Long Long |
Long integer type |
64 |
Float |
Single-precision floating-point number |
6-digit valid digits |
Double |
Double-precision floating-point number |
10-digit valid digits |
Long double |
Extended double-precision floating-point numbers |
10-digit valid digits |
Empirical guidelines for data type selection
- When the known value cannot be negative, the unsigned type is selected
using int to perform integer operations, short is often too small, as long and int are generally sized. If int is not enough, use a long long.
Do not use char or bool in an arithmetic expression, and using char is particularly prone to problems. If you want to use a small integer, then explicitly execute as signed char or unsigned char.
Performing a floating-point operation with Double,float is usually not enough and the cost of both is comparable. Long Double is generally not necessary, and consumption cannot be neglected.
Signed types and unsigned types
- An unsigned type assignment exceeds its range, and the result is a modulo value. such as unsigned char c =-1; Assume that the value of char for 8BIT,C is 255
- The signed type assignment is beyond its scope and the result is undefined . such as signed char C2 = 256; Assume that the value of char 8bit,c2 is undefined
- Never mix signed and unsigned types.
- There are both unsigned and signed numbers in an arithmetic expression, and the signed number is converted to an unsigned number
Specify the type of literal value
Character and string literals |
Prefix |
Meaning |
Type |
U |
Unicode16 characters |
char16_t |
U |
Unicode32 characters |
char32_t |
L |
Wide character |
wchar_t |
U8 |
UTF-8 (for string literal constants only) |
Char |
Integer literals |
Floating-point literals |
U or U |
Unsigned |
F or F |
Float |
L or L |
Long |
L or L |
Long double |
ll or LL |
Long Long |
|
|
Initialize and assign values
initialization and assignment are two completely different operations .
- An object of a built-in type defined in a function body (block scope) whose value is undefined if it is not initialized. Develop the habit of initializing built-in variables.
- If the object of a class is not explicitly initialized, its value is determined by the class.
Declarations and definitions
- The declaration makes a name known to the program, the definition requests storage space, and it may assign an initial value
(Split compilation)
If you want to declare a variable rather than define it, add the keyword extern before the variable name and do not explicitly initialize the variable
- For complex declaration statements, you can understand the variable name from right to left
Pointer
- Common error: Dereference invalid pointer (null pointer or released pointer)
- Recommendation: Initialize all pointers, and then define a pointer to it as soon as you define the object.
- Recommended nullptr instead of 0,0 pointers are ambiguous.
Const and CONSTEXPR
Const objects are valid within a file by default, when multiple files appear with the same name as a const variable, which is equivalent to defining separate variables in different files.
If you want to share a const between different files, add the extern keyword regardless of the declaration or definition.
The top-level const represents itself as a constant, and the underlying const means that the object pointed to by the pointer is a constant.
- When performing object copy operations, the top-level const is unaffected, but the underlying const cannot be ignored
constant Expression : An expression in which the value does not change and can evaluate the result at compile time.
- It is difficult to tell whether initialization is a constant expression in a complex system, and C11 allows the compiler to verify that it is declared as constexpr.
Although pointers and references can be defined as constexpr, their initial values are strictly limited. The initial value of the constexpr pointer must be nullptr or 0, or an object stored in a fixed address. Objects stored outside the function body have fixed addresses that can be used to initialize the constexpr pointer.
Auto and Decltype
- Auto generally ignores the top-level const while preserving the underlying const.
The Decltype expression is a variable, then Decltype returns the type of the variable, including the top-level const and the reference.
- Decltype If the expression used is not a variable, the type that corresponds to the result of the expression is returned.
If the expression is a dereference operation, the reference type is obtained.
If it is a variable with double brackets, the result is always a reference.
preprocessing variables ignore scope-scoped rules in C + +
Base----variables and basic types