C和C++的静态变量之间的区别?

RAJ*_*MAR 0 c c++ static non-static

在C中它是一个错误

int x=5;
static int y=x; //error
Run Code Online (Sandbox Code Playgroud)

在C++中它有效为什么?

int x=5;
static int y=x; //valid
Run Code Online (Sandbox Code Playgroud)

Mik*_*our 5

因为C和C++是不同的语言.

程序启动时,C++具有动态初始化阶段,其中静态变量可以使用非平凡构造函数或非常量初始化函数进行初始化.C没有,并且需要使用常量表达式初始化静态变量.