It was thus said that the Great David Riley once stated:
On Jul 2, 2012, at 9:18 PM, Sean Conner wrote:
It was thus said that the Great David Riley once
stated:
On Jul 2, 2012, at 6:53 PM, Sean Conner wrote:
> AFAIK sizeof(unsigned short) is not defined
anywhere :-).
I'm not sure I follow you here. In C, you can indeed do a
sizeof(unsigned short)
and get back the size (in characters) of a short int (with the size in bits
of a character defined by CHAR_BIT).
But it's not defined in a standard, which is a problem. It could be 128
bits when it's compiled for all you know.
Um .. it *IS* defined in a standard---the ANSI C standard:
... Their implementation defined values shall be equal or greater in
magnitude (absolute value) to those shown, with the same sign.
CHAR_BIT - number of bits for smallest object that is not a
bit-field (byte)
CHAR_BIT 8
I should clarify. Yes, you can get sizeof(whatever) and it will be
the size of whatever in bytes; I meant that sizeof(unsigned short)
is undefined by a standard. Assuming that it has even a fixed
minimum (1 char notwithstanding) is a fool's errand in a lot of
cases. 32-bit architectures have dominated computing since I
started programming, but I've still seen plenty of breakage (even
in new code) caused by such assumptions.
But sizeof(unsigned short) is *still* defined (same section as above).
The ANSI C specification states that a short shall be *at least* 16 bits in
size, but can be larger, but must be shorter than a long. If the compiler
isn't ANSI C, then yes, it can be pretty much anything, but ANSI does
specify a minimum length.
And for the record:
char 8 bits or larger [1]
short 16 bits or larger
long 32 bits or larger
An int can't be shorter than a short, and can't be longer than a long.
-spc (At least we get signed/unsigned integers in C; in Java, no such
luck)
[1] a plain 'char' declaration can be signed or unsigned, depending upon
compiler.