Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

sizeof(long) >= 4

sizeof(int) >= 2

sizeof(long) >= sizeof(int)

That's all you can expect.



You literally cannot expect the first two. On word-addressable hardware, you could easily get sizeof(long) = sizeof(int) = 1. This happens on DSPs.


Yes. Didn't think about machines where the memory isn't composed of bytes.

The best approach is still to not expect anything.


I understand some implementations might break this rule, but what standard states this?


(Derived from C11, specifically N1570).

§5.2.4.2.1

CHAR_BIT is at least 8

USHRT_MAX is at least 2^16 - 1

UINT_MAX is at least 2^16 - 1

ULONG_MAX is at least 2^32 - 1

ULLONG_MAX is at least 2^64 - 1

§6.2.5p8 guarantees that integers of increasing conversion ranks obeys subset relations, and §6.3.1.1 lays out that short, int, and long have conversion ranks in that order.

Thus, sizeof(char) = 1 ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long).


All of these were present in C99 as well.


Also long long >= 8, per C99.


Assuming CHAR_BIT is 8, of course.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: