Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sizeof(int) <= sizeof(long) <= sizeof(long long) always true?

From C standard, int has of at least 16bit, long has of at least 32bit and at least 64bit for long long if any (some platforms may not support). Just wondering if the sentence as title is always true.

like image 420
c-stranger Avatar asked Nov 28 '22 15:11

c-stranger


2 Answers

No. The standard only defines the minimum ranges for each of those types. Conceivably int could have a 16-bit range, but 48 bits of padding, bringing it to 64-bits (8 bytes, if CHAR_BITS == 8), while long is 32-bits (4 bytes).

Of course, this would be silly. But it's not forbidden, as such.

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

like image 183
bdonlan Avatar answered Dec 06 '22 23:12

bdonlan


According to C Programming/Reference Tables, particularly the Table of Data Types:

int ≥ 16 ≥ size of short

long ≥ 32 ≥ size of int

long long ≥ 64 ≥ size of long

As bdonlan pointed out, this only refers to the range of the values, not the size in memory (which sizeof returns in bytes). The C standard doesn't specify the size in memory that each type can use, so it's left to the implementation.

like image 44
Bill the Lizard Avatar answered Dec 07 '22 00:12

Bill the Lizard