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
|
|
DEMO: /home/cs255001/demo/tmp/demo.c
|
I will "unpack" this statement with some examples
Consider the following program with 2 variables with name x:
#include <stdio.h> int main( int argc, char* argv[] ) { int x = 4; // Scope of x starts here { int x = 7; // x is overshadowed by x here } } |
One variable x=4 is in the outer scope and another variable x=7 is in the inner scope
Example of scoping:
#include <stdio.h>
int main( int argc, char* argv[] )
{
int x = 4; // Scope of x starts here
printf("%d\n", x); // prints 4
{
int x = 7; // x is overshadowed by x here
}
}
|
The variable x in printf("%d\n", x); refers to int x = 4;
Example of scoping:
#include <stdio.h>
int main( int argc, char* argv[] )
{
int x = 4; // Scope of x starts here
{
printf("%d\n", x); // prints 4
int x = 7; // x is overshadowed by x here
}
}
|
The variable x in printf("%d\n", x); refers to int x = 4;
Example of scoping:
#include <stdio.h> int main( int argc, char* argv[] ) { int x = 4; // Scope of x starts here { int x = 7; // x is overshadowed by x here printf("%d\n", x); // prints 7 !!! } } |
The variable x in printf("%d\n", x); refers to int x = 7;
Example of scoping:
#include <stdio.h>
int main( int argc, char* argv[] )
{
int x = 4; // Scope of x starts here
{
int x = 7; // x is overshadowed by x here
} // End of overshadowing !
printf("%d\n", x); // prints 4
}
|
The variable x in printf("%d\n", x); refers to int x = 4;
Example of scoping error:
#include <stdio.h> int main( int argc, char* argv[] ) { printf("%d\n", x); // Error: x undefined int x = 4; // Scope of x starts here { int x = 7; // x is overshadowed by x here } } |
The variable x was used before the start of the scope of the variable x !
Example of scoping error:
#include <stdio.h> int main( int argc, char* argv[] ) { // Outer scope int x; { // Inner scope int y = 4; x = y + 3; } printf( "%d %d\n", x, y ); // Error } |
The variable y was used after the end of the scope of the variable y !
|
Where can you use (= access) a local variable in C:
|
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 !!!
Parameter variables are defined in the function header:
void funcName( int param1, float param2 )
{
int localVar1;
float localVar2;
...
}
|
What is the scope and life time of parameter variables ?
The scope and life time of parameter variables are same as local variables that are defined at the start of the function:
void funcName( |
And we have just discussed the scope and life time of local variables...