220 likes | 406 Views
STORAGE CLASS. 4 Storage classes. Automatic variables External Static Register variables. Automatic variables. Are declared inside a function in which they are to be utilized. Are created when function is called And destroyed when function is exited.
E N D
4 Storage classes • Automatic variables • External • Static • Register variables
Automatic variables • Are declared inside a function in which they are to be utilized. • Are created when function is called • And destroyed when function is exited. • Are private or local to function in which they are declared. • Variable declared inside a function without storage class is by default automatic variable.
Main() { Int number; ------- ------ } Main() { Auto int number; ---- } Automatic variables value cannot be changed accidentally by what happens in some other function in the program. Use same variable name in different functions in the same program without any confusion to compiler.
Main() { int m=1000; function2(); printf(“%d\n”,m); } Function1() { int m=10; Printf(“%d\n”,m); } Function2() { int m=100; Function1(); printf(“%d\n”,m); } O/P: 10 100 1000
2 consequences of scope and longevity of auto variables. First any variable local to main will normally live throughout the whole program, although it is active only in main. Main() { intn,a,b; scope level 1 ---- if(n<=100) { intn,sum; scope level 2 ---- } -----/* sum not valid here*/ -----
Variables n,a,b defined in main have scope from beginning to end of main. However, variable n defined in main cannot enter into block of scope level 2 bcoz scope level 2 contains another variable named n. Second ‘n’ is available only inside scope level 2 and no lo0nger available the moment control leaves the ‘if’ block. ‘Sum’ defined in ‘if’ block is not available outside that block.
External variables • Variable that are both alive and active throughout the entire program. • Known as global variables. • Can be accessed by any function in the program. • Are declared outside a function.
int number; Float length=7.5; Main() { ---- ---- } Function1() { ---- ---- } Function2() { ----- ---- }
Variables number and length are available for use in all 3 functions. • In case a local variable and global variable have same name, the local variable will have precedence over the global one in the function where it is declared. int count; main() { count=10; --- } function() { int count=0; -- count=count+1; } When the function references variable ‘count’, it will reference only local variable not global one. Value of ‘count’ in main is not affected.
Int x; Main() { x=10; printf(“x=%d\n”,x); printf(“x=%d\n”,fun1()); printf(“x=%d\n”,fun2()); printf(“x=%d\n”,fun3()); } Fun1() { x=x+10; return(x); }
fun2() { int x; x=1; return(x); } Fun3() { x=x+10; return(x); } Output: x=10 x=20 x=1 x=30 Once variable declared global, any function can use it and cannot change its value. Subsequent functions can refer only that new value.
Main() { y=5; ---- } Int y; Func1() { y=y+1; } As far main is concerned, ‘y’ is not defined, so compiler issue error message. The statement y=y+1; in func1() will Therefore assign 1 to y.
Static variable • The value of static variable persists until the end of the program. static int x; static float y; Static variable may be either internal or external type depending on place of declaration. Internal static variables are declared inside a function. Similar to auto variables. Internal static variables can be used to retain values between function calls.
main() { int I; for(i=1;i<=3;i++) stat(); } Stat() { static int x=0; x=x+1; printf(“x=%d\n”,x); } O/P: x=1 x=2 x=3
Static variable is initialized only once, when program is compiled. • It is never initialized again. • During the first call to stat, x is incremented to 1. • Bcoz x is static, this value persists and therefore , the next call adds another 1 to x giving it a value of 2. • Value of x becomes 3 when 3rd call is made.
If we declare x as an ‘auto’ variable, the output would be x=1 x=1 x=1 This is bcoz each time stat is called, the auto variable x is initialized to 0. When the function terminates, its value of 1 is lost.
External static variable is declared outside of all functions and is available to all the functions in that program. • Difference b/w static external variable and simple external variable is that static external is available only within the file where it is defined while simple external variable can be accessed by other files.
Register variables • Variable should be kept in one of the machines registers, instead of keeping in the memory. • Register access is faster than memory access. • So frequently accessed variables are kept in registers. register int count; Compilers allow only int or char variables to be placed in register.