r/C_Programming • u/heavymetalmixer • Sep 11 '26
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?
6
Upvotes
View all comments
Show parent comment
1
u/aioeu Sep 12 '26 edited Sep 12 '26
staticvariables declared at file scope have internal linkage;staticvariables declared inside a function have no linkage.But it's not the identifiers' linkage that matters here. What matters is the storage duration of the objects. A
staticvariable declared at file scope and astaticvariable declared within a function both identify objects with static storage duration, which means in each case the object's lifetime (the time during which it exists and maintains its last-stored value) is the entire execution of the program.(Linkage is really all about when and how multiple declarations of an identifier can refer to the same object, i.e. in the way those identifiers are linked.)