From my research, these were the only guarantees that I could find about these built-in integer data types:
char will not be smaller than 8 bitsshort will not be smaller than 16 bitsshort will not be smaller than charIn practice, according to my experience, short is always double the size of char, but it does not appear that this is a guarantee made by the C language specification. It seems that a scenario such as a 16 bit char and a 16 bit short could still be valid.
This is more of a curiosity question, I realize that if you really need exact widths for your integer data types, you're better off using the stdint.h header.
UPDATE 5/6/2021: I do not believe this question is a duplicate of What platforms have something other than 8-bit char?. I am not asking about the specific size of a char, but rather the ratio between a char and a short, regardless of the actual size of either one. At least for me, many of these answers have enlightened me in a way that the "duplicate" question has not. I hope this also helps others that stumble upon it.
As a few people have already pointed out, the specification makes no guarantees about the "bit widths" of built in types, it's left up to the implementation of the compiler for the given architecture how these are defined.
What's maybe more interesting is how this gets used in practice. In fact there are "current" architectures which have built-in sizes that don't follow the usual 1-2-4-8 conventions. A good example of this (and the one that I have a fair amount of personal experience with) is the Analog Devices SHARC.
From the Wikipedia page:
it knows nothing of 8-bit or 16-bit values since each address is used to point to a whole 32-bit word, not just an octet. It is thus neither little-endian nor big-endian, though a compiler may use either convention if it implements 64-bit data and/or some way to pack multiple 8-bit or 16-bit values into a single 32-bit word. Analog Devices chose to avoid the issue by using a 32-bit char in their C compiler.
A 32-bit character could definitely be called a "pathological" case, but it works just fine for the SHARC, which is widely used in professional and prosumer audio applications. See Universal Audio's Apollo as an example.
In theory, all of char, short, int could have the same size.
Very few implementations are doing that in practice (I cannot name any; I think in the previous century you might have found some)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With