c++ - LONG_MAX Cygwin 的大小

标签 c++

我用 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

最佳答案

你为什么这么确定LONG_MAX != 9223372036854775807?这对我来说似乎很好,因为 2^63 -1 == 9223372036854775807。这意味着长整型在您的实现中是 64 位,第一位用作符号位。请记住,标准并未具体指定所有不同数据类型的大小 - 在 Visual Studio 的编译器上,long 与 int 的大小相同,这有点烦人;)

关于c++ - LONG_MAX Cygwin 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30288804/

相关文章:

c++ - 我的文件没有被正确复制

c++ - 如何在Qt C++中使用C函数

使用 std::future 的 C++ 程序拒绝在 Windows XP Embedded 上运行

c++ - O_EXCL 写出来的字是什么

c++ - 左移一个 'double' 操作数

c++ - Winsock c++,程序在连接到离线服务器时卡住

C++ 在单词中查找字谜

c++ - 创建嵌套类的实例

c++ - Xcode,Objective-C 项目 : not compiling C++ file

c++ - Visual Studio 使用 vc90.pdb 做什么?