Possible Duplicate:
What is the difference between an int and a long in C++?
#include <iostream>
int main()
{
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(long int) << std::endl;
}
Output:
4
4
How is this possible? Shouldn't long int
be bigger in size than int
?
The guarantees you have are:
sizeof(int) <= sizeof(long)
sizeof(int) * CHAR_BITS >= 16
sizeof(long) * CHAR_BITS >= 32
CHAR_BITS >= 8
All these conditions are met with:
sizeof(int) == 4
sizeof(long) == 4
C++ langauge never guaranteed or required long int
to be bigger than int
. The only thing the language is promising is that long int
is not smaller than int
. In many popular implementations long int
has the same size as int
.
It depends on the language and the platform. In ISO/ANSI C, For example, the long integer type is 8 bytes in 64-bit system Unix, and 4 bytes in other os/platforms.
No. It's valid under the C standard for int
to be the same size as short int
, for int
to be the same size as long int
, for int
not to be the same as either long int
or short int
, or even for all three to be the same size. On 16-bit machines it was common for sizeof(int)
== sizeof(short int)
== 2 and sizeof(long int)
== 4, but the most common arrangement on 32-bit machines is sizeof(int)
== sizeof(long int)
== 4 and sizeof(short int)
== 2. And on 64-bit machines you may find sizeof(short int)
== 2, sizeof(int)
== 4, and sizeof(long int)
== 8.
See http://bytes.com/topic/c/answers/163333-sizeof-int-sizeof-long-int.
No nothing is wrong. The size of data-types is generally hardware dependant (exception being char which is always 1 register wide). In gcc/Unix, int and long int both require 4 bytes. You can also try sizeof(long long int) and see the results and sizeof(short int).
精彩评论