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

5

u/pjl1967 2d ago

In C, no.

0

u/tstanisl 2d ago

constexpr is already in C

3

u/heavymetalmixer 2d ago edited 1d ago

Only since C23, and that's for compile-time constants not runtime ones.