c++ - 为什么 long 和 long long 的最大值产生相同的输出?

标签 c++ c++11 visual-c++ c++14 c++builder

#include <iostream>
using namespace std;
#include <climits>

int main () {

    int n_int = INT_MAX;
    short n_short = SHRT_MAX;
    long n_long = LONG_MAX;
    long long n_llong = LLONG_MAX;

    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 << "llong: " << n_llong << endl << endl;
    //cout << endl;

    cout << "Minimum int value = " << INT_MIN << endl;
    cout << "Maximum int value = " << INT_MAX << endl;
    cout << "Bits per byte = " << CHAR_BIT << endl;

    return 0;
}

输出: long 和 long long 变量产生完全相同的最大值,我不确定为什么。 long值应该输出最大值2147483647。

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
llong: 9223372036854775807
Minimum int value = -2147483648
Maximum int value = 2147483647
Bits per byte = 8

最佳答案

long 不保证是 32 位长;它只需 至少 32 位长。您的实现已将 long 定义为具有 64 位。请注意,尽管 longlong long 在您的实现中具有相同的范围,但它们仍然是不同的类型。

关于c++ - 为什么 long 和 long long 的最大值产生相同的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45993050/

相关文章:

c++ - Win32 Window Wrapper 错误(参数不正确)

c++ - C++中的不同对象

c# - P/Invoke - 结构参数的简单添加

c++ - 有没有更惯用的方法来替换指针?

c++ - 如何访问网络文件?

c++ - C 正则表达式 VS C++11 正则表达式中的不同行为

c++ - 我怎样才能部分特化枚举值的类模板?

c++ - 连接字符和字符串文字

c++ - 使用 MS Visual C++ 2010 'sys/resource.h' : No such file 编译 C

visual-studio - Visual C++ 中多个项目之间的链接器错误