c++ maximum of number types
Possible Duplicate:
size of int, long, etc
Is `long` guaranteed to be at least 32 bits?
I wanted to find out maximum of each data type for my computer. the code is :
int main() {
using namespace std;
cout << numeric_limits<int>::max() << endl;
cout << numeric_limits<long>::max() << endl;
cout << numeric_limits<long long>::max() << endl;
return 0;
}
which prints:
2147483647
2147483647
9223372036854775807
question 1: why int
and long
are same?
question 2: above output is from VS2010 on 64bit. Is my c++ program running as 64bit?
question 1: why int and long are same?
For various reasons (convenience), sizes of integers supported by machine architectures tend to be powers of two. Most modern processors can natively work with 8, 16, 32, and 64-bit integers. However, there are five commonly used integer types: char
, short
, int
, long
, and long long
. So two of them have to have the same size.
On most 16-bit platforms, int
and short
are both 16-bit.
On most 32-bit platforms, int
and long
are both 32-bit.
On most 64-bit platforms, long
and long long
are both 64-bit. There is one exception...
question 2: above output is from VS2010 on 64bit. Is my c++ program running as 64bit?
Impossible to tell from this data. Windows is the one platform where long
and int
have the same size for both 32-bit and 64-bit programs.
They output the same because with Visual Studio, both int
and long
are signed 32-bit integers. This is true regardless of whether you're building a 64-bit or 32-bit binary (Windows 64-bit follows the LLP64 model)
Because the size of int
and long
doesn't vary between 32-bit and 64-bit on visual studio, there's no way to tell which you're buinding from the data you've provided
The maximum value of a given type is compiler dependant. The C++ standard does not state anything about a long having to be a specific number of bits etc.
However what is does state is that:
1 = sizeof(char)<=sizeof(short)<=sizeof(int)<=sizeof(long)<=sizeof(long long)
If you are looking to use a specific sized integer, I would suggest including "inttypes.h" and using the int_8t, int16_t, int32_t, int64_t, etc...
链接地址: http://www.djcxy.com/p/40398.html上一篇: 什么是64位GCC上int的最大值
下一篇: c ++数字类型的最大值