Afternoon all,
I've got a horribly simply question, that has been bugging me for a long time - I'm familiar with the usage of static functions, to limit visibility of static functions to within a given translation unit and to avoid exposing functions outside of a single source file that do not need to be, and the same principle applied to variables also to limit the usage of global vars, including MISRA (and general best-practice) rules on limiting usage of global variables etc.
However, there is a compiler behavior which has always given me a headache, understanding why it behaves this way...
Simple example (compiled on Windows using gcc just to prove the point...):
An example 'module' in our application, with a static variable and static function.
#include "module.h" static int var = 30; static void module_add(void){ printf("Adding 20 to var...\n"); var += 20; printf("Added Var = %d\n", var); }
#ifndef MODULE_H_ #define MODULE_H_ #include <stdio.h> static int var; static void module_add(void); #endif /* MODULE_H_ */
If I try and call said function from main (or any other translation unit), I expect the compiler to fail
#include "module.h" #include <stdio.h> void subtract(void); int main(void){ subtract(); module_add(); } void subtract(void){ printf("Var = %d\n", var); var -= 5; printf("Subtracted Var = %d\n", var); }
Which it does... exactly as expected due to an undefined reference to module_add()...
undefined reference to `module_add'
If I change this slightly though, so that module_add() is not static, I would expect this to still not compile, because var which is also referenced in main, is also static.
#include "module.h" static int var = 30; void module_add(void){ printf("Adding 20 to var...\n"); var += 20; printf("Added Var = %d\n", var); }
#ifndef MODULE_H_ #define MODULE_H_ #include <stdio.h> static int var; void module_add(void); #endif /* MODULE_H_ */
But instead, the behavior I get is a successful compilation, and when run I get the following printed out into the command window...
Var = 0 Subtracted Var = -5 Adding 20 to var... Added Var = 50
Now the actual value of var is clearly hidden from main here (but not module.c, as expected), given it's printed as =0 when called from main, then as =50 when called from module_add(), so that is what I would expect... but, the question I've never been able to get out of my mind is this:
a) why does this compile, I've always thought this should throw a compiler error, such as below - because var is now completely 'hidden' from main!?
error: 'var' undeclared (first use in this function)
b) does this mean the subtract() function is effectively creating it's own local, automatic variable called 'var', of some type (I assume int, whose width may vary depending on target architecture), and as per rules of C is initializing that variable to 0. If so, again - why no error? I can't just go about using undeclared variables any other time, so why should this work?
I appreciate I'm probably missing the point, but it seems like inconsistent behavior between declaring a var or function static. Before I'm shot - I appreciate static has different meanings depending on how it's called, an that a local static variable will persist between function calls etc...
OR - is this specific to gcc?