Global and static variables can only be initialized with constant expressions known at compile time. Calling your foo()
function does not constitute using a constant expression. Further, the order in which global and static variables are initialized is not specified. Generally, calling foo()
would mean that there must be a certain order, because the function can reasonably expect some other variables to be already initialized.
IOW, in C, neither of your code is executed before main()
.
In C++ there are ways around it, but not in C.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…