Global variables are defined outside all functions:
int x; // Global variable int y = 4; // Global variable void f( ) { int y; x = y + 3; } int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
C's global variables are similar to Java's class (static) variables
The lexical scope (or scope) of a global variable is:
int x; // Global variable int y = 4; // Global variable void f( ) { int y = 7; // Overshadows global var y x = y + 3; // Uses y = 7 } int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); // Uses y = 4 } |
in any program file starting from the place of definition or variable declaration until the end of the program file --- unless it is overshadowed by a local variable (with the same name)
Example of scoping error with a global variable:
int x; // Global variable void f( ) { x = y + 3; // Use of y outside it's scope } int y = 4; // Global variable // Scope of y int main( int argc, char* argv[] ) { printf( "%d %d\n", x, y ); } |
When
y
was used, the
variable definition
was not yet processed !
Remember that the
C-compiler is
1-pass !!!
|
|
|
A global variable can be defined after it was used:
#include <stdio.h> int x; // Global variable void f( ) { x = y + 3; // Compile error: y undefined !! } int y = 4; // Global variable y (defined too late) int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
The C compiler needs to know the data type of y when it translates x = y + 3!!!
Declaring the variable y will solve this compile error:
#include <stdio.h> int x; // Global variable extern int y; // Declare variable y void f( ) { x = y + 3; // Fixed: y is declared !! } int y = 4; // Global variable y int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
The C compiler knows the data type of y when it translates x = y + 3!!!
Another example of scoping error with a global variable:
progFile1.c | progFile2.c |
---|---|
#include <stdio.h>
int main( int argc, char* argv[] )
{
f( );
printf( "%d %d\n", x, y );
}
|
int x; // Global variable int y = 4; // Global variable int f( ) { x = y + 3; } |
You will get this error when you compile progFile1.c: 'x' undeclared and 'y' undeclared
The C compiler can not process the program file progFile1.c because the data type of x and y are unknown !
Fix with declarations:
progFile1.c | progFile2.c |
---|---|
#include <stdio.h> extern int x, y; int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
int x; // Global variable int y = 4; // Global variable int f( ) { x = y + 3; } |
DEMO: /home/cs255001/demo/tmp/demo.c + demo2.c
Apply the scoping rule on x and y to understand that the declaration makes this program correct
The life time of a global variable is:
#include <stdio.h> int x; // Global variable int y = 4; // Global variable void f( ) { x = y + 3; } int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
Global variables are "permanent" variables
Their life time is the entire duration of the program execution
You can have multiple declarations for the same global variable (as long the declarations do not conflict with each other):
#include <stdio.h> int x; extern int y; // Declare y extern int y; // Declare y again (no conflict) void f( ) { x = y + 3; } int y = 4; // Global variable int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
Declarations of global variables can be written inside a function:
#include <stdio.h>
int x;
void f( )
{
extern int y; // Declare y
x = y + 3;
}
int y = 4; // Global variable
int main( int argc, char* argv[] )
{
f( );
printf( "%d %d\n", x, y );
}
|
Then the declarations are subjected to the scoping rule of local variables !
Example of declaration and scoping rule:
#include <stdio.h> int x; void f( ) { { extern int y; // declaration } x = y + 3; // Error: y is undeclared ! } int y = 4; // Global variable int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
The scope of declaration of variable y ended in the inner scope !!