University of Kelaniya Sri Lanka Scope, Lifetime and Storage Class of a Variable COSC 12533/ COST 12533 SACHINTHA PITIGALA 2017 - Sachintha Pitigala < 1
What is Scope? Scope of Identifier: The scope of an identifier (or named constant) is the region of program code in which it is legal to use that identifier for any purpose. < 2
Scope of a Variable There are two places where variables can be declared in C programming language: 1. Inside a function or a block which is called local variables 2. Outside of all functions which is called global variables. < 3
Local Variables Variables that are declared inside a function are called local variables. They can be used only by statements that are inside that function. Local variables are not known to functions outside their own. < 4
Local Variables: Examples < 5
More Examples: In C, you can reuse names, as long as they are not in overlapping scopes. void main ( ) { int i = 5, j = 0; for (j = 0; j < 10; j++) { int i = j; // OK, this is new i int k = 5; dosomething (i); int sum = k; // compile error, no k in scope j = i; // sets j to 5 for (j = 0; j < 100; j++ ) { int i = j; // yet another new i int i = 0; // compile error redefined variable < 6
Global Variables The global variables need to be declared outside of all functions. Global variables are available from the point of declaration to the end of the entire file containing the program code. Try to minimize global scope: Only use global variables if you really, really have to!!! < 7
Example: int i = 10; void main ( ) { i =5; for (j = 0; j < 10; j++ ) { int i = 20; i=70; i=90; int i = 30; i=100; < 8
Lifetime of a Variable The lifetime of a variable is the time during program execution in which a variable actually has memory allocated to it. < 9
Lifetime of Local Variables Their storage is created (allocated) when control enters the function. Local variables are alive while function is executing. Their storage is destroyed (deallocated) when function exits. < 10
Lifetime of Global Variables Their lifetime is the lifetime of the entire program. Their memory is allocated when program begins execution. Their memory is deallocated when the entire program terminates. < 11
Storage Classes in C There are four types of storage classes in C: 1. Automatic 2. Static 3. Extern 4. Register However, we will talk only the first three storage classes in this course. < 12
Automatic Storage Class Automatic variables are declare inside a function in which they are to be utilized. Automatic variables are declared using a keyword auto. eg. auto int number; These variables are created when the function is called and destroyed automatically when the function is exited. These variables are therefore private(local) to the function in which they are declared. < 13
Automatic Storage Class auto is the default storage class for all local variables. { int Count; auto int Month; The example above defines two variables with the same storage class. auto can only be used within functions, i.e. local variables. < 14
Static Storage Class The value of static variables persists until the end of the program. It is declared using the keyword static like static int x; static float y; Static variables are initialized only once. < 15
Static Class Example: #include <stdio.h #include <stdlib.h void increment(); void main() { increment(); increment(); increment(); void increment() { static int i=1; #include <stdio.h #include <stdlib.h void increment(); void main() { increment(); increment(); increment(); void increment() { static int i=1; printf("%d\t",i); printf("%d\t",i); i++; Output: 1 1 1 i++; Output: 1 2 3 < 16
External Storage Class These variables are declared outside any function. These variables are active and alive throughout the entire program. Also known as global variables and default value is zero. Unlike local variables they are accessed by any function in the program. In case local variable and global variable have the same name, the local variable will have precedence over the global one. Sometimes the keyword extern used to declare these variable. < 17
External declaration: example void main() { extern int y;...... void func1() { extern int y;...... int y; Note that extern declaration does not allocate storage space for variables. < 18