Storage Classes: Global & Local Variables
Storage Classes: Global & Local Variables
Global & Local Variables Variables declared in programs may be classified into two categories Local variables Global variables In the programs written earlier variables are declared inside of the functions. Such variables are called local variables, variables may be declared outside the scope of a function such variables are called global variables.
Local variables
Local
variables scope is confined within the block or function where it is defined. ie. they are recognized within the block or the function If the local variable is not initialized in the declaration it is initialized with garbage by the system As soon as the execution of the block starts the local variable becomes active and at the end of the execution it is destroyed.
Local variables-example
main() { int i=4;
int j=10; i++; if (j > 0) { int i=100; printf("value of i inside the block = %d\n",i); } printf("value of I outside the block= %d\n",i);
} Output value of i inside the block = 100 value of i outside the block=5
Global Variables
Scope of the global variables is from the point of declaration through the remainder of the program. i.e. any function which falls in their scope can access their values. If same variable name is being used for global and local variable then local variable takes preference in its scope. But it is not a good practice to use same name for both global and local variable.
example
int i=4; main() { i++; /* Global definition */
func(); printf( "Value of global varaible i = %d\n", i ); } func() { int i=10; /* Local declaration of i */ i++; printf( "Value of local variable in the function func i = %d\n", i ); } Output Value of local variable in the function func i = 11 Value of global variable i = 5
Storage classes
Each Variable in C is characterized by its data type storage class Data type refers the type of data to be stored in a variable. For example integer variables used to store integer data items, float variables used to store real numbers int x,y; // x and y are integer variables and represent integer data float r; // r is a float variable and represent floating point type data
Storage classes
Storage class refers to the Scope of a variable longevity of a variable default initial value memory location The scope actually determines the portion of the program that, the variable is recognized. The lifetime of the variable determines how long the variable is alive or active.
Storage classes
There are four different storage classes in C and they are represented by the keywords
auto
extern static register
Storage classes
The general form of variable declaration with storage class is Storage_class data_type variable_name1, variable_name2,..; For example auto int x; static float y .
Automatic variables The automatic variables are declared inside a function or a block void main() { auto int x,y; //x and y are automatic variables . .. }
Automatic variables However, local variables without any storage class specifier have the default storage class auto void fun1() { int c,k; variables } ..
Automatic variables Scope: The scope of automatic variables is local to the function in which it is defined.
Life time: The lifetime of automatic variable is temporary. i.e. it is defined when a function is called for execution and is destroyed when the control is exited from its defining function
Automatic variables
void main( ) { int i; for(i=1; i<5; i++) printf(%d\n, fun1( )); } int fun1( ) { x=10; return(++x); }
Automatic variables Default initial value: The default initial value of automatic variable is garbage Memory location: These variables are located in main memory.
Automatic variables Note1: Variable defined in one function is not accessible in any other function.
void main() { int x=25; printf(\n x=%d,x); } void fun1() {int y=50; Printf(\n x=%d,x); Printf(\n y=%d, y); }
Automatic variables If the name of global and local variables is same then in the scope of local variable the first priority is for local variables void main() { int x=25; printf(\n x=%d,x); } void fun1() x- is local to main( ) {int y=50; but not accessed in fun1( ) Printf(\n x=%d,x); Printf(\n y=%d, y); }
Automatic variables int x = 100; main() {int a; printf(\n x=%d,x); if(x>=100) { int x = 10; printf(\n x=%d,x); } Printf(\n x=%d, x); }
External variables Variables that are declared outside the scope of any function are external variables. All global variables are external variables. int s; void main() { s=96; // s is global variable .. } float r; void fun1() { r=5.4; // r is global variable }
External variables Scope: The scope of external variables extends from the point of definition through the remainder of the program Life time: The lifetime of global variables is permanent i.e. they retain their values throughout the execution of the program Default initial value: The default initial value of external variable is zero. External variables cannot be initialized with expressions for example, int a=5, b=10, c=a+b; is not allowed, if a,b,c are external variables Memory location: These variables are located in main memory
int x=96; void main( ) {printf(\n x=%d,x); fun1(); fun2(); printf(\n x=%d,x); } void fun1( ) { x=200; printf(\n x=%d, x); } void fun2() { printf(\n x=%d,x); x=500; }
If a global variable is accessed before its point of definition then it needs to be declared as it is an external variable using the key word extern void main( ) {extern int X; External variable declaration (memory is not allocated) printf(\n X=%d, X ); fun1(); } int X; External variable definition (memory is allocated) void fun1() Output { X=200 X=200 X=200; printf(\n X=%d, X ); }
Static variables For some applications it is needed to retain the value of a variable even after the control is exited from the function. For this purpose variables of static storage class are used. Static variables are initialized only once even though the function may be called more than one time. Static variables may be for internal variables or external variables. Internal static variables are declared inside a function using the key word static. Whereas external static variables are declared outside of a function using the key word static.
Static variables static int c; // c is static external int s; // s is external void main() { static auto int x; //x is static internal variable int i,j; // i and j are automatic variables . .. } void fun1() { static int c,k; // c and k are static internal variable .. }
Static variables Scope: The scope of static internal is local to the function in which it is defined and the scope of static external variable is from the point of declaration through the remainder of the program Life time: The lifetime of static variables is permanent. Internal static variables retain their values even if the control is exited from the function Default initial value: The default initial value of internal or external static variable is zero Memory location: Both these variables are located in main memory.
return(++x);
Examples for Static variables Output static int x; x=1 void main( ) y=5 { static int y; printf(\n x=%d, ++x); printf(\n y=%d, y+=5); } Note :Default initial value is zero
Register variables A variable is usually stored in the main memory It is possible to tell the compiler that a variable should be kept in the CPU registers by defining it as register variable. Since a register access is much faster than memory access, keeping the frequently accessed variables (like loop control variables) in the register will make the execution of the program faster.
Register variables Except the memory space allocation all the other properties of register variables are similar to that of automatic variables.
Very few variables can be placed in the register. Normally two or three per function. Only local variables of type int, char can be declared with the keyword register
Register variables void main( ) { register int i; // i is register variable. for(i=0; i<=10000; i++) { } .. ... }
Register variables Scope: The scope is local to the function in which it is defined. Life time: The life time of register variables is temporary Default initial value: The default initial value is garbage Memory location: These variables are located in registers of CPU