Trouble with printf conversion long long

心已入冬 提交于 2020-01-14 14:20:12

问题


I've been working on a project euler problem, which by their very nature coerce you to use data types with big storage.

#include <stdio.h>
#include <conio.h>
#define num 600851475143

int main()
{
long long i, j, count=0, number=num, k;
for(i=2;number!=1;i++)
{
    count=0;
    for(j=1;j<=i;j++)
    {
        if((i%j)==0)
        {
            count++;
        }
    }
    for(k=0;k<100000000;k++)
    {}
    if(count==2)
    {
        printf("                      %d\n", i);
        if(number%i==0)
        {
            number/=i;
            printf("      %d\n", number);
            printf("%d\n", i);
            i=2;
        }
    }
}
getch();
return 0;
}

When I compile and run the program, there is nothing printed for number. I have tried various printf conversions %ll, %l, I have changed data types. I am using GNU GCC compiler. What should I do?


回答1:


You should (re)read the documentation, I guess.

%ll didn't work since ll is not a complete specifier, it's just a modifier for the actual conversion specifier, which should follow.

Try %lld.




回答2:


The correct format for printf is %lld. Moreover you should use a prefix for your constant num, because this integer constant is too large to be hold in long type.

#define num 600851475143LL

Perhaps should you avoid lower-case macro's identifiers?



来源:https://stackoverflow.com/questions/13194067/trouble-with-printf-conversion-long-long

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