Why both long and int are 4 bytes

If you have tested the size of long and int built-in types, when you find that they are both 4 bytes, you may have questions. If they are all 4 bytes, long does not provide a larger storage range, then what is the use of long? ? The following content of this article will solve these doubts for you.

First, briefly outline the development of the following built-in types. If there are omissions, please correct me. At the beginning of the C language, there were only two kinds of char (8-bit) and int (16-bit). Later, short (16-bit) and long (32-bit) were added with the development. At this time, int can be 16-bit or 32-bit. Depends on the platform and subsequent compatibility. Later, when 64-bit appeared, long long (64-bit) was added. In order to standardize, some adjustments were made to the range of smaller types, gradually stabilizing to int32-bit, long can have multiple definitions, It can be 32-bit or 64-bit.

The C++ standard only says that long must be at least as large as int, and all integer types must meet the following specifications when implemented:

   sizeof(char)  ==  1

   sizeof(char)  <=  sizeof(short)

   sizeof(short)  <=  sizeof(int)

   sizeof(int)  <=  sizeof(long)

   sizeof(long)  <=  sizeof(long long)

Except for char and long long, the other types are more flexible and are platform-dependent and implementation-dependent. If you want to achieve platform independence, on the windows platform, __intn can be used, and n represents the number of bits. __int8 __int16 __int32 __int64.

Guess you like

Origin blog.csdn.net/qq_31433709/article/details/109026311