C++ 整数常量对于“long”类型来说太大了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5541560/
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
integer constant is too large for "long" type
提问by Bacu
Possible Duplicate:
long long in C/C++
可能的重复:
C/C++ 中的 long long
Writing a simple program for a project Euler problem. Refuses to compile because "integer constant is too large for "long" type", even though it should be well within the size limits of an unsigned long long. Using the dev-c++ compiler.
为项目欧拉问题编写一个简单的程序。拒绝编译,因为“整数常量对于“long”类型来说太大了”,即使它应该在unsigned long long的大小限制内。使用 dev-c++ 编译器。
code in question:
有问题的代码:
#include <iostream>
bool isprime (unsigned long long i)
{
if(i==1||i==0) return false;
if(i==2) return true;
for(unsigned long long k=2;k!=i-1;k++)
{
if(i%k==0) return false;
}
return true;
}
int main()
{
for(unsigned long long i=600851475143;i>=0;i--) //problematic line
{
if(isprime(i))
{
std::cout<<i;
std::cin.get();
return 0;
}
}
}
回答by Fred Larson
Try an "ULL" suffix: 600851475143ULL
试试“ULL”后缀: 600851475143ULL
回答by Mark B
Your literal as typed has type int
which isn't big enough to hold the value. Try 600851475143ULL
as a first fix.
您输入的文字的类型int
不足以容纳该值。尝试600851475143ULL
作为第一次修复。
Note even with that, your for
loop will never terminate since an unsigned can never be less than 0. Instead, use a long long
and 600851475143LL
.
请注意,即使如此,您的for
循环也永远不会终止,因为 unsigned 永远不会小于 0。相反,请使用 along long
和600851475143LL
。
回答by FumbleFingers
Must be a limitation of dev-c++ support for long longdatatype. It compiles fine on MS VC++ 2010.
必须是 dev-c++ 支持long long数据类型的限制。它在 MS VC++ 2010 上编译得很好。