【发布时间】:2015-05-17 15:47:56
【问题描述】:
我用 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 是 4 个字节。 短是 2 个字节。 long 是 8 个字节。 long long 是 8 个字节。 最大值: 诠释:2147483647 简称:32767 长:9223372036854775807 长长:9223372036854775807 最小 int 值 = -2147483648 每字节位数 = 8预期输出:
int 是 4 个字节。 短是 2 个字节。 long 是 8 个字节。 long long 是 8 个字节。 最大值: 诠释:2147483647 简称:32767 长:2 147 483 647 长长:9223372036854775807 最小 int 值 = -2147483648 每字节位数 = 8【问题讨论】:
-
您使用的是 64 位系统吗?你检查过吗?
sizeof(long)? -
@Hornise。你为什么说这是不正确的?你期望的结果是什么?
-
另外,如果您正在使用 C++ 编程,您可能希望改用
std::numeric_limits。 -
是的,我做到了,没有任何改变。
-
您能否编辑您的问题以包含 actual 和 expected 输出?最重要的是,是
sizeof(long)?
标签: c++