tags:

views:

782

answers:

5

from C standard, int has of at least 16bit, long has of at least 32bit and at least 64bit for long long if any (some platforms may not support). Just wondering if the sentence as title is always true.

Thanks.

+10  A: 

Yes it is always true.


To clarify, in this here reality with which I'm familiar this is always true. If you want to win a bar bet you can refer to bdonlan's learned answer but if you ever come across an implementation in which sizeof(int) > sizeof(long) you have my permission to give the implementer a good thrashing.

Motti
Citation?
bdonlan
Haha, +1 for definitive tone.
Hooked
+14  A: 

No. The standard only defines the minimum ranges for each of those types. Conceivably int could have a 16-bit range, but 48 bits of padding, bringing it to 64-bits (8 bytes, if CHAR_BITS == 8), while long is 32-bits (4 bytes).

Of course, this would be silly. But it's not forbidden, as such.

Note, however, that sizeof(char) == 1, by definition. So sizeof(char) <= sizeof(anything else).

bdonlan
Are you sure about sizeof(char) <= sizeof(anything else)?
Martinho Fernandes
Given that the unit of sizeof is sizeof(char) (it returns an integer factor of sizeof(char), by definition), I'd hope so.
Falaina
@martinho, sizeof(char)==1, and sizeof never returns zero or negative, so...
bdonlan
While C++ requires `sizeof(int) <= sizeof(long)` etc, I think C doesn't, and it in fact allows `sizeof(int) > sizeof(long)` etc because of padding as far as i know. But both require that `INT_MAX <= LONG_MAX` etc.
Johannes Schaub - litb
My understanding if the same as litb (C++ requires it, C lacks the formal rules requiring it even if it would be a very strange implementation not to respect that inequality)
AProgrammer
+2  A: 

Practical C++ Programming says that

C++ guarantees that the storage for short <= int <= long

Still searching for long long.

Pete
That's C++. The question asked about C and the C standard. They don't have to agree.
Thomas Owens
Silly me didn't notice the "C" tag for this question... sorry for the C++ reference!
Pete
And you should understamnd what any particular text book tells you may well not be true. Text books do not define a languge - international standards do.
anon
Falaina
anyway I think it's a useful information.
fortran
by the way, you can quit searching. long long is not part of C++ yet. :)
jalf
anon
+5  A: 

According to C Programming/Reference Tables, particularly the Table of Data Types:

int ≥ 16 ≥ size of short

long ≥ 32 ≥ size of int

long long ≥ 64 ≥ size of long

As bdonlan pointed out, this only refers to the range of the values, not the size in memory (which sizeof returns in bytes). The C standard doesn't specify the size in memory that each type can use, so it's left to the implementation.

Bill the Lizard
Those are bits, not bytes, and thus refers to the range of the type - unless sizeof(int) really >= 16 on your machine :). Does this apply to the results of the sizeof() operator as well?
bdonlan
@bdonlan: Thanks, I see the distinction now. I edited my answer to be as correct as it can be. :)
Bill the Lizard
+1  A: 

At least for ISO C++, this is well-defined (excepting long long for obvious reasons) by the Standard in 3.9.1[basic.fundamental]/2:

There are four signed integer types: “signed char”, “short int”, “int”, and “long int.” In this list, each type provides at least as much storage as those preceding it in the list.

Note that this is speaking of storage, not value ranges. This specifically means sizeof.

Pavel Minaev