C++ long long 和 long 和有什么不一样

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6462439/
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-28 20:11:30  来源:igfitidea点击:

What's the difference between long long and long

c++intlong-integer

提问by Hikari Iwasaki

What's the difference between long long and long? And they both don't work with 12 digit numbers (600851475143), am I forgetting something?

long long 和 long 和有什么不一样?而且它们都不适用于 12 位数字 (600851475143),我是不是忘记了什么?

#include <iostream>
using namespace std;

int main(){
  long long a = 600851475143;
}

回答by Joey Adams

Going by the standard, all that's guaranteed is:

按照标准,所有保证的是:

  • intmust be at least 16 bits
  • longmust be at least 32 bits
  • long longmust be at least 64 bits
  • int必须至少为 16 位
  • long必须至少为 32 位
  • long long必须至少为 64 位

On major 32-bit platforms:

在主要的 32 位平台上:

  • intis 32 bits
  • longis 32 bits as well
  • long longis 64 bits
  • int是 32 位
  • long也是 32 位
  • long long是 64 位

On major 64-bit platforms:

在主要的 64 位平台上:

  • intis 32 bits
  • longis either 32 or 64 bits
  • long longis 64 bits as well
  • int是 32 位
  • long是 32 位或 64 位
  • long long也是 64 位

If you need a specific integer size for a particular application, rather than trusting the compiler to pick the size you want, #include <stdint.h>(or <cstdint>) so you can use these types:

如果您需要特定应用程序的特定整数大小,而不是相信编译器会选择您想要的大小,#include <stdint.h>(或<cstdint>)以便您可以使用这些类型:

  • int8_tand uint8_t
  • int16_tand uint16_t
  • int32_tand uint32_t
  • int64_tand uint64_t
  • int8_tuint8_t
  • int16_tuint16_t
  • int32_tuint32_t
  • int64_tuint64_t

You may also be interested in #include <stddef.h>(or <cstddef>):

您可能还对#include <stddef.h>(或<cstddef>)感兴趣:

  • size_t
  • ptrdiff_t
  • size_t
  • ptrdiff_t

回答by Cheers and hth. - Alf

long longdoes not exist in C++98/C++03, but does exist in C99 and c++0x.

long long在 C++98/C++03 中不存在,但在 C99 和 c++0x 中存在。

longis guaranteed at least 32 bits.

long保证至少 32 位。

long longis guaranteed at least 64 bits.

long long保证至少 64 位。

回答by Karl Knechtel

To elaborate on @ildjarn's comment:

详细说明@ildjarn 的评论:

And they both don't work with 12 digit numbers (600851475143), am I forgetting something?

而且它们都不适用于 12 位数字 (600851475143),我是不是忘记了什么?

The compiler looks at the literal value 600851475143without consideringthe variable that you're assigning it to/initializing it with. You've written it as an inttyped literal, and it won't fit in an int.

编译器查看文字值600851475143而不考虑您将其分配给/初始化它的变量。你已经把它写成一个int类型化的文字,它不适合int.

Use 600851475143LLto get a long longtyped literal.

使用600851475143LL获得long long的类型化的文字。

回答by Craig White

Your C++ compiler supports long long, that is guaranteed to be at least 64-bits in the C99 standard (that's a C standard, not a C++ standard). See Visual C++ header file to get the ranges on your system.

您的 C++ 编译器支持 long long,在 C99 标准(这是 C 标准,而不是 C++ 标准)中保证至少为 64 位。请参阅 Visual C++ 头文件以获取系统上的范围。

Recommendation

推荐

For new programs, it is recommended that one use only bool, char, int, and double, until circumstance arises that one of the other types is needed.

对于新程序,建议只使用 bool、char、int 和 double,直到出现需要其他类型之一的情况。

http://www.somacon.com/p111.php

http://www.somacon.com/p111.php

回答by msathia

Depends on your compiler.long long is 64 bits and should handle 12 digits.Looks like in your case it is just considering it long and hence not handling 12 digits.

取决于你的编译器。long long 是 64 位,应该处理 12 位数字。看起来在你的情况下它只是考虑它很长,因此不处理 12 位数字。