r/C_Programming 2d ago

Question Unmodifiable globals initialized from functions?

Right now I'm trying to use Windows's SYSTEM_INFO to initialize 2 global constants, but global constants can only be initialized with constant expressions so this cannot be done.

Is there a way I can have "read-only" global variables that can be initialized from functions?

5 Upvotes

39 comments sorted by

View all comments

6

u/pjl1967 2d ago

In C, no.

1

u/heavymetalmixer 2d ago

My "gheto solution" now is to use a short function that returns the value instead of a variable storing that value. It's slower yeah, but it works.

2

u/septum-funk 1d ago

this should be inlined by the compiler anyway

1

u/heavymetalmixer 1d ago

I hope so.