Sizeof (int)> = 2 and sizeof (long)> = 4: Is this always true for any implementation?
B.Stroustrup says the following in section 6.2.8 of his new book (4th edition TCPL):
Some aspects of basic C ++ types, such as the size of int, are implementationdefined (ยง6.1).
Later, on page 150, we have the following example:
int_least16_t y; // at least 2 bytes (just like int)
int_least32_t yy // at least 4 bytes (just like long)
My interpretation of these two comments is that the size int
(or a long
) is implementationdefined, but their minimum sizes are always 2 and 4 bytes respectively. That is sizeof(int) >= 2
, sizeof(long) >= 4
for any implementation.
First, is it correct? If so, where is it specified in the Standard?
source to share
No, this is not always the case. It is possible that char
is a 64bit type, in which case sizeof(int)
and sizeof(long)
can be equal to 1.
However, you are guaranteed that sizeof(int) * CHAR_BIT >= 16
and sizeof(long) * CHAR_BIT >= 32
because int
and long
require at least 16 and 32 bits, respectively.
source to share
First: No, this is wrong. Since it is wrong, the standard does not say that it is so ...;)
[If by bytes we are referring to 8bit units, and not to size units char
as the specification does, then it is true that it int_least16_t
is at least 2 bytes since it must be at least 16 bits, and of course 4 bytes for is int_least32_t
also true with 8bit bytes, but the C and C ++ specs say nothing about whether char
which is considered the smallest and is often used interchangeably with byte [ sizeof(char)
guaranteed to yield 1
]]
The only condition that the standard has:

sizeof(char) <= sizeof(int) <= sizeof(long)
[at least]. 
int
can contain at least 16 bits, andlong
can contain at least 32 bits.
All implementations that follow this are fine. The system can have char
which is 24 bits, and sizeof ( int
) == 1 for 24bit int
and sizeof(long) == 2
for 48bit long
is perfectly valid. Or one where all types are 32, 40, 48, 64, 80, 96 or 128 bits.
source to share