Sizeof (int) <= sizeof (long) <= sizeof (long long) is always right?

From the C standard, int has at least 16 bits, a length of at least 32 bits, and at least 64 bits over time, if any (some platforms may not support). Just wondering if the sentence is true as a headline.

+4
source share
5 answers

Not. The standard defines only the minimum ranges for each of these types. Presumably, int may have a 16-bit range, but 48 bits of padding, bringing it to 64 bits (8 bytes if CHAR_BITS == 8), and a long one is 32-bit (4 bytes).

Of course that would be stupid. But this is not prohibited, as such.

Note, however, that sizeof(char) == 1 , by definition. So sizeof(char) <= sizeof( anything else ) .

+18
source

Yes, it is always true.


To clarify, in this reality that I am familiar with, this is always true. If you want to win a bet in a bar, you can refer to bdonlan , but if you have ever come across an implementation in which sizeof(int) > sizeof(long) you have permission to give the developer a good beating.

+10
source

According to C Programming / Reference Tables , in particular Data Type Table :

int ≥ 16 ≥ size of short

long ≥ 32 ≥ size int

long ≥ 64 ≥ size of long

As bdonlan pointed out, this applies only to the range of values, and not to the size in memory (sizeof size is returned in bytes). The C standard does not specify the size in memory that each type can use, so it remains for implementation.

+6
source

Practical C ++ programming says that

C ++ ensures that the storage for short <= int <= long

Continuation of the search is long .

+2
source

At least for the C ++ ISO, this is well defined (with the exception of long long for obvious reasons) by the Standard in 3.9.1 [basic.fundamental] / 2:

There are four signed integer types: signed char, short int, int and long int. In this list, each type contains at least as much memory as the ones preceding it in the list.

Note that this is about storage, not about values. This specifically means sizeof .

+2
source

All Articles