The number of bits of memory occupied by the data type is related to the number of bits of the operating system and the compiler. Generally, in the current mainstream compilers, the int type is 4 bytes in either 32-bit or 64-bit systems.
【Recommended course: C Language Tutorial】
The compiler can choose the appropriate size based on its own hardware, but it needs to meet constraints: short and int types are at least 16 bits, long type is at least 32 bits, and the length of short type cannot exceed int type, and int type cannot exceed long type. . This means that the variable length of each type is determined by the compiler. In current mainstream compilers, the int type in 32-bit machines and 64-bit machines is generally 4 bytes (for example, GCC). The following lists the number of bytes occupied by each type of variable under the GCC compiler on 32-bit machines and 64-bit machines:
C类型 | 32 | 64 |
char | 1 | 1 |
short int | 2 | 2 |
int | 4 | 4 |
long int | 4 | 8 |
long long int | 8 | 8 |
char* | 4 | 8 |
float | 4 | 4 |
double | 8 | 8 |
Summary: The number of bits a data type occupies in memory is actually related to the number of bits in the operating system and the compiler (the number of bits supported by different compilers may be different). Specifically, the number of bytes occupied by a certain data type The compiler needs to coordinate the number of bits in the operating system before allocating the memory size
The above is the detailed content of How many bytes does int occupy?. For more information, please follow other related articles on the PHP Chinese website!