Hermann Speiche
Hermann Speiche

Reputation: 924

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

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?

Upvotes: 15

Views: 457

Answers (3)

Keith Thompson
Keith Thompson

Reputation: 263237

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.

Upvotes: 13

Niklas B.
Niklas B.

Reputation: 95298

I assume that the Wikipedia article is referring to the value range of those integer types, rather than their size (as reported by sizeof).

From C99 standard draft N1256:

6.3.1.1/1

The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

6.2.5/8

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.

Upvotes: 10

pmg
pmg

Reputation: 108978

From C2011 Standard

6.3.1.1/1

— The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

Upvotes: 6

Related Questions