integer constant is too large for “long” type [duplicate]

大憨熊 提交于 2019-11-30 00:46:29

问题


Possible Duplicate:
long long in C/C++

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.

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;
        }
    }
}

回答1:


Try an "ULL" suffix: 600851475143ULL




回答2:


Your literal as typed has type int which isn't big enough to hold the value. Try 600851475143ULL as a first fix.

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.




回答3:


Must be a limitation of dev-c++ support for long long datatype. It compiles fine on MS VC++ 2010.



来源:https://stackoverflow.com/questions/5541560/integer-constant-is-too-large-for-long-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!