Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"int" really required to be at least as large as "short" in C?

Tags:

c

standards

c99

c89

I've read a couple of times in different sources (e.g. Wikipedia: http://en.wikipedia.org/wiki/C_variable_types_and_declarations#Size), that in C, a long long is not smaller than a long, which is not smaller than an int, which is not smaller than a short.

However, I've looked this up in the C90 and C99 standards, and haven't found a corresponding clause. I've found only that C90 and C99 specifiy the minimum type sizes (Section 5.2.4.2.1 in C90 and C99 standards), but not their sizes in relation to each other. Have I missed something in the standards?

like image 209
Hermann Speiche Avatar asked May 27 '12 13:05

Hermann Speiche


1 Answers

6.3.1.1 defines the relative integer conversion ranks of any two integer types. This is an abstract concept that's meant only to define the relationship between two types; there is no value defined as the rank of any type.

6.2.5p8 says:

For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.

It doesn't say anything about their relative sizes, and in fact it's theoretically possible for a conforming (but deliberately perverse) implementation to have sizeof (short) > sizeof (int). This is possible only if short has more padding bits (bits that don't contribute to the value) than int. This is very unlikely; most implementations don't have padding bits at all, and I know of no implementations where the relationships of the ranges of integer types differ from the relationships of their sizes.

Reference: either N1256, the latest C99 draft, or N1570, the latest C2011 draft.

like image 103
Keith Thompson Avatar answered Oct 03 '22 01:10

Keith Thompson