Quote:
Originally Posted by just_wondering
What are global variables?
|
It might help to know what NOT global variables are as well.
There are different degrees of 'global'-ness and it's generally more useful and precise to think in terms of the 'scope' of variables, rather than global or not.
Consider this simple C function
Code:
int foo (int x, int y){
int z = x + y;
return z;
}
Here 'z' is an integer variable.
It's both declared and assigned a value in the same line.
More importantly, because it's declared inside the body of a function, it is said to have 'function' scope
This essentially means that 'z' is a variable that is almost as unglobal as a variable can be (there exists even more restrictive scope, but ignore that fact for now).
No other function can use or change the value of the variable 'z'.
In fact, 'z' only even exists during the brief period that function 'foo' is executing.
Now look at a relatively more global variable, one that can be used by two or more functions.
Code:
int z;
void add1(void){
z = z + 1;
}
void subtract1(void){
z = z - 1;
}
Because 'z' is declared outside the body of any function, it is said to have (at least) file scope.
This 'z' can be accessed by any function that exists in the same source file.
It is also possible to make 'z' visible to functions in other source files, giving it what some call 'program' scope.
So, now that you have some idea what global means in the context of variables, google for "scope variables" and start becoming a programmer.
And have fun with it.