When compiling this sample code
#include <stdio.h> #include <stdlib.h> int myfunc() { printf("Constructor\n"); return 1; } static const int dummy = myfunc(); int main() { printf("main\n"); return 0; }
it works when compiled as C ++, but not as C using the same compiler (MingW gcc). I get initializer element is not constant in C mode.
Therefore, apparently, there are differences regarding static initialization. Is there a reason why this is apparently allowed for C ++, but not for C? Is it because otherwise you cannot have global objects with constructor functions?
source share