5 ms·
Can we stop pretending that the default C integer types have any kind of definitive size? That is wrong, and it's just asking for more portability bugs down the
by marcosdumay 10y ago
Can we stop pretending that the default C integer types have any kind of definitive size? That is wrong, and it's just asking for more portability bugs down the line.
The correct sizes:
- char >= 1 byte
- int, short >= 2 bytes
- long >= 4 bytes
- long log >= 8 bytes
You can not rely on any further information about them.
- peapicker 10y agoYou can't even rely on that... long --> 4 bytes in 32 bit builds everywhere I've used. long --> 4 bytes in 64 bit builds on windows and OS/400, 8 bytes in almost all 64 bit builds on UNIXs that I've used (HPUX,Solaris,Linux,AIX).
- AnimalMuppet 10y agoIn marcosdumay's comment, those were greater-than-or-equals, not arrows.
- lanna 10y agoIt says it there on the page: "It depends on OS by OS but, most commonly…"
- marcosdumay 10y agoIt also varies from one compiler to another, and on a few different versions on the same compiler. It also varies on the same OS, same compiler but different target architecture. And that's now. Who knows if it won't vary due to compiler optimizations in the future? It is wrong to rely on anything more than what it says on the standard.
- jwilk 10y agochar is 1 byte exactly.
- John23832 10y agoA char is 1 byte on most systems, but the actual standard defines it as having CHAR_BITS number of bits. Modern C defines CHAR_BITS as CHAR_BITS >= 8. Some older machines have 7 bits. Theres nothing stopping machines from changing what CHAR_BITS is. <Limits.h>
- kps 10y agoPeople are talking past each other. In the C standard ‘byte’ means minimum addressable unit — the unit of sizeof — which need not be exactly 8 bits (but in ANSI C must be at least 8 bits). In C, 'char' is one byte, always and exactly. It's also possible for 'long' to be one byte, provided CHAR_BIT≥32.
- John23832 10y ago> In the C standard ‘byte’ means minimum addressable unit — the unit of sizeof Do you have a reference? I've always seen a byte defined as 8 bits. No matter what. (Unless you're talking to a hard drive manufacturer, that's a different bucket of worms though.)
- kps 10y agoOrginally? See §7.4 of Planning a Computer System: Project Stretch, edited by Werner Buchholz who coined the term. http://archive.computerhistory.org/resources/text/IBM/Stretch/pdfs/Buchholz_102636426.pdf http://archive.computerhistory.org/resources/text/IBM/Stretc... For C, see C11 §3.6: “byte — addressable unit of data storage large enough to hold any member of the basic character set of the execution environment” http://www.iso-9899.info/n1570.html#3.6 http://www.iso-9899.info/n1570.html#3.6
- John23832 10y agoMakes sense now. Thanks
- jwilk 10y ago
- pklausler 10y ago> You can not rely on any further information about them. Not entirely true. One may assume that sizeof(short) <= sizeof(int) <= sizeof(long). In particular, one may cast from short to int and back without loss of information, and likewise from short to long and from int to long.
- GFK_of_xmaspast 10y agoAnybody who cares about integer sizes in any more detail needs to use things like int32_t.