C/C++ 中的 long long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1458923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:03:11  来源:igfitidea点击:

long long in C/C++

c++typeslong-integer

提问by sud03r

I am trying this code on GNU's C++ compiler and am unable to understand its behaviour:

我正在 GNU 的 C++ 编译器上尝试这段代码,但无法理解它的行为:

#include <stdio.h>;

int main()
{
    int  num1 = 1000000000;
    long num2 = 1000000000;
    long long num3;
    //num3 = 100000000000;
    long long num4 = ~0;

    printf("%u %u %u", sizeof(num1), sizeof(num2), sizeof(num3));
    printf("%d %ld %lld %llu", num1, num2, num3, num4);
    return 0;
}

When I uncomment the commented line, the code doesn't compile and is giving an error:

当我取消注释注释行时,代码不会编译并给出错误:

error: integer constant is too large for long type

错误:整数常量对于长类型来说太大

But, if the code is compiled as it is and is executed, it produces values much larger than 10000000000.

但是,如果代码按原样编译并执行,它会产生远大于 10000000000 的值。

Why?

为什么?

回答by unwind

The letters 100000000000 make up a literal integer constant, but the value is too large for the type int. You need to use a suffix to change the type of the literal, i.e.

字母 100000000000 构成了一个文字整数常量,但该值对于类型来说太大了int。您需要使用后缀来更改文字的类型,即

long long num3 = 100000000000LL;

The suffix LLmakes the literal into type long long. C is not "smart" enough to conclude this from the type on the left, the type is a property of the literal itself, not the context in which it is being used.

后缀LL使文字变成 type long long。C 不够“聪明”,无法从左侧的类型得出结论,该类型是文字本身的属性,而不是使用它的上下文。

回答by Arkaitz Jimenez

Try:

尝试:

num3 = 100000000000LL;

And BTW, in C++ this is a compiler extension, the standard does not define long long, thats part of C99.

顺便说一句,在C++中这是一个编译器扩展,标准没有定义long long,那是C99的一部分。

回答by sellibitze

It depends in what mode you are compiling. long long is not part of the C++ standard but only (usually) supported as extension. This affects the type of literals. Decimal integer literals without any suffix are always of type int if int is big enoughto represent the number, long otherwise. If the number is even too big for long the result is implementation-defined (probably just a number of type long int that has been truncated for backward compatibility). In this case you have to explicitly use the LL suffix to enable the long long extension (on most compilers).

这取决于您编译的模式。long long 不是 C++ 标准的一部分,但仅(通常)作为扩展支持。这会影响文字的类型。如果 int 大到足以表示数字,没有任何后缀的十进制整数文字总是 int 类型,否则为 long。如果数字对于 long 来说太大,则结果是实现定义的(可能只是为了向后兼容而被截断的 long int 类型的数字)。在这种情况下,您必须明确使用 LL 后缀来启用 long long 扩展(在大多数编译器上)。

The next C++ version will officially support long long in a way that you won't need any suffix unless you explicitly want the force the literal's type to be at least long long. If the number cannot be represented in long the compiler will automatically try to use long long even without LL suffix. I believe this is the behaviour of C99 as well.

下一个 C++ 版本将以不需要任何后缀的方式正式支持 long long ,除非您明确希望文字的类型至少为 long long。如果数字不能用 long 表示,编译器将自动尝试使用 long long 即使没有 LL 后缀。我相信这也是 C99 的行为。

回答by Omry Yadan

your code compiles here fine (even with that line uncommented. had to change it to

您的代码在这里编译正常(即使该行未注释。必须将其更改为

num3 = 100000000000000000000;

to start getting the warning.

开始收到警告。