The C standard DOES specify the meaning of "char," "short," "int," and so forth. char is "the smallest addressable unit" (in practice always 1 byte), short and int are at least 2 bytes, and long is at least 4 bytes. It may not seem important to you now, but saving a few bytes definitely mattered back in the 1970s, when C was designed.
It would be nice for the stdint.h types to be built-in, and more widely used in some cases, but it's really not a big deal. There are always higher-level languages you can use if you don't want to deal with this stuff. Some of them even have unlimited length integers! The 70s are over, you know.
Posted Jun 27, 2012 13:49 UTC (Wed) by nix (subscriber, #2304)
[Link]
char is "the smallest addressable unit" (in practice always 1 byte)
It's not a matter of 'in practice'. char is, by definition, one byte long. In closely adjacent paragraphs of every C Standard ever published, sizeof() is defined as yielding 'the size (in bytes) of its operand', and then sizeof (char) is defined as returning 1. If you change sizeof (char), you change the definition of a byte on that platform's C ABI.
Pettenò: Debunking x32 myths
Posted Jun 27, 2012 14:22 UTC (Wed) by mansr (guest, #85328)
[Link]
While char is always and by definition one byte, a byte is not always one octet.
Pettenò: Debunking x32 myths
Posted Jun 27, 2012 21:27 UTC (Wed) by nix (subscriber, #2304)
[Link]
Exactly. Of course, on any reasonable platform these days (other than tiny DSPs normally not programmed in C at all), a byte is one octet, but in olden days this was not true. Of course by the time C was standardized at all it was pretty widely true, and by the time of C99 it was universal. But it wasn't always so.
Pettenò: Debunking x32 myths
Posted Jun 27, 2012 21:45 UTC (Wed) by mansr (guest, #85328)
[Link]
POSIX does mandate 8-bit bytes. As for those DSPs, a lot of code running on them is actually written in C. Only the critical parts are typically done in assembly.
Pettenò: Debunking x32 myths
Posted Jun 29, 2012 4:57 UTC (Fri) by sethml (subscriber, #8471)
[Link]
Check out the TI C2000 / TMS320 architecture. I've written a lot of C++ with 16-bit chars on that architecture over the past few years. Yes, it's a bit of a brain-dead architecture, but there are definitely archs with non 8 bit chars around still.
Pettenò: Debunking x32 myths
Posted Jun 29, 2012 12:11 UTC (Fri) by nix (subscriber, #2304)
[Link]
At least that's a multiple of 8. Are there any 9/18/36-bit bytes around these days? I hope not :}