为什么这段代码在C和C++下编译结果不一样?

#include <stdio.h>

int foo()
{
    return 50;
}

int main()
{                                        
    static int a = foo();
    printf("%d\n", a);


    return 0;
}  

上面这段代码,我用 g++ 编译可以通过,程序也可以正常输出 50,
用 gcc 编译就编译失败了,为什么?

阅读 2k
1 个回答

c99标准里

4 All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题