In C language, I want to access a global static variable outside the scope of the file. Let me know the best possible way to do it.
One of the methods is to assign an extern global variable the value of static variable,
In file a.c
static int val = 10;
globalvar = val;
In file b.c
extern globalvar;
But in this case any changes in val(file a.c) will not be updated in globalvar in (file b.c).
Please let me know how can I achieve the same.
Thanks,
Sikandar.
Well, if you can modify file
a.cthen just makevalnon-static.If you can modify
a.cbut can’t makevalnon-static (why?), then you can just declare a global pointer to it ina.cand in
b.cdowhich will let you access the current value of
valthrough*pval. Or you can introduce a non-static function that will return the current value ofval.But again, if you need to access it from other translation units, just make it non-static.