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

浏览:57日期:2023-04-15

问题描述

#include <stdio.h>int foo(){ return 50;}int main(){ static int a = foo(); printf('%dn', a); return 0;}

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

问题解答

回答1:

c99标准里

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

相关文章: