我从C ++ Primary Plus编写了关于变量大小的程序 这是代码:
#include <iostream>
#include <climits>
using namespace std;
int main() {
int n_int = INT_MAX;
short n_short = SHRT_MAX;
long n_long = LONG_MAX;
long long n_llong = LLONG_MAX;
// wielkosc
cout << "int is " << sizeof (int) << " bytes." << endl;
cout << "short is " << sizeof n_short << " bytes."<< endl;
cout << "long is " << sizeof n_long << " bytes." << endl;
cout << "long long is " << sizeof n_llong << " bytes." << endl;
cout << endl;
cout << "Maximum values: "<< endl;
cout << "int: " << n_int << endl;
cout << "short: " << n_short << endl;
cout << "long: " << n_long << endl;
cout << "long long: " << n_llong<< endl << endl;
cout << "Minimum int value = " << INT_MIN << endl;
cout << "Bits per byte = " << CHAR_BIT << endl;
return 0;
}
当我在Eclipse IDE中由Cygwin编译并运行时告诉我LONG_MAX = 9223372036854775807,这不是真的,我做错了什么?感谢。
实际输出:
int is 4 bytes. short is 2 bytes. long is 8 bytes. long long is 8 bytes. Maximum values: int: 2147483647 short: 32767 long: 9223372036854775807 long long: 9223372036854775807 Minimum int value = -2147483648 Bits per byte = 8
预期输出:
int is 4 bytes. short is 2 bytes. long is 8 bytes. long long is 8 bytes. Maximum values: int: 2147483647 short: 32767 long: 2 147 483 647 long long: 9223372036854775807 Minimum int value = -2147483648 Bits per byte = 8
答案 0 :(得分:2)
你为什么这么肯定LONG_MAX!= 9223372036854775807?这对我来说似乎很好,因为2 ^ 63 -1 == 9223372036854775807.这意味着你的实现中long是64位,第一位用作符号位。请记住,标准并没有详细说明所有不同数据类型应该有多大 - 在Visual Studio的编译器上,long与int的大小相同,这有点烦人;)