开发者

Representation of Long Integers [duplicate]

开发者 https://www.devze.com 2023-02-11 00:24 出处:网络
This question already has answers here: 开发者_StackOverflow中文版 Closed 11 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow中文版 Closed 11 years ago.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消