When a variable is defined inside some function:
#include <stdio.h> int main( int argc, char* argv[] ) { int x; // Local variable int y = 4; // Local variable x = y + 3; printf( "%d %d\n", x, y ); } |
the variable is a local variable
The lexical scope (or scope) of a local variable is:
#include <stdio.h> int main( int argc, char* argv[] ) { int x; // Local variable int y = 4; // Local variable x = y + 3; printf( "%d %d\n", x, y ); } |
starting from the place of definition till the end of the scope that contains the local variable definition
The Example of scoping error:
#include <stdio.h> int main( int argc, char* argv[] ) { int x; // Local variable x = y + 3; int y = 4; // Local variable printf( "%d %d\n", x, y ); } |
The variable y was used before the start of the scope of the variable y !
Where can you use (= access) a local variable:
|
Purpose of local variables:
|
The life time of a local variable is:
#include <stdio.h> int main( int argc, char* argv[] ) { int x; // Local variable int y = 4; // Local variable x = y + 3; printf( "%d %d", x, y ); } |
From the time when the function starts running till the time when the function returns
Remember from assembler programming: local variables are created on the runtime stack !!!
When a variable is defined outside all functions:
#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 ); } |
the variable is a global variable
The lexical scope (or scope) 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 ); } |
in any program file starting from the place of definition or variable declaration till the end of the program file
Example of scoping error with a global variable:
#include <stdio.h> int x; // Global variable void f( ) { x = y + 3; } int y = 4; // Global variable int main( int argc, char* argv[] ) { f( ); 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 !!!
|
|
|
We have discussed this in the following webpage: click here
#include <stdio.h> int x; // Global variable void f( ) { x = y + 3; // Compiler needs type info on y !! } int y = 4; // Global variable int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
Example of variable declaration (only use with global variables):
#include <stdio.h> int x; extern int y; // Declaring a variable // Starts the scope of variable y ! void f( ) { x = y + 3; } int y = 4; // Global variable int main( int argc, char* argv[] ) { f( ); printf( "%d %d\n", x, y ); } |
When y was used, the variable declaration was processed - no errors !
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: '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