Difference between initialization of static variables in C and C++

It compiles in C++ because C++ needs to support dynamic initialization anyway, or you couldn’t have local static or non-local objects with non-trivial constructors.

So since C++ has this complexity anyway, supporting that initialization like you show isn’t complicated to add anymore.

In C that would be a big matter because C doesn’t have any other reason to support initialization done at program startup (apart from trivial zero initialization). In C, initial values of file-scope or local static objects can always statically be put into the executable image.

Leave a Comment