Is there any difference between global and extern variables in c?
global variables are those defined outside a function body
if declared static they are global to the file otherwise are global to the program, e.g.
-
int x=10; // global to program
-
static int y; // global to file from this point onwards
-
-
int func()
-
{
-
int z; // local variable to function
-
..
-
}
-
note global variables are initialised to 0 if not explicity initialised (as x was). Local variables are not initialised unless you explicity do so.
to access a global variable in another file you use the keyword extern, e.g.
-
extern int x; // access x defined in another file
-