首页 > 技术文章 > Dev-C++中stdio.h对long long的兼容问题

stevechekblain 2016-06-22 18:26 原文

使用stdio.h的代码:

#include<stdio.h>
int main()
{
    long long n,s;
    scanf("%lld",&n);
    s=n*(n+1)*(2*n+1)/6;
    printf("%lld",s);
    return 0;
}

运行结果:

2
-715827878
--------------------------------
Process exited after 1.664 seconds with return value 0
请按任意键继续. . .

使用cstdio的代码:

#include<cstdio>
int main()
{
    long long n,s;
    scanf("%lld",&n);
    s=n*(n+1)*(2*n+1)/6;
    printf("%lld",s);
    return 0;
}

运行结果:

2
5
--------------------------------
Process exited after 1.259 seconds with return value 0
请按任意键继续. . .

但将long long改为long就正常了。

从资料中可以查阅到, long long是在C++11被正式加入的, 此前其只在C99中可用, 所以使用旧版的头文件就可能出错。

结论:在c++中使用stdio.h时要注意long long的兼容问题。

推荐阅读