Download presentation
Presentation is loading. Please wait.
1
STORAGE CLASS
2
4 Storage classes Automatic variables External Static
Register variables
3
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.
4
Main() { Int number; ------ } 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.
5
Main() { int m=1000; function2(); printf(“%d\n”,m); } Function1() int m=10; Printf(“%d\n”,m); Function2() int m=100; Function1(); O/P:
6
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() { int n,a,b; scope level if(n<=100) int n,sum; scope level 2 } -----/* sum not valid here*/ -----
7
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.
8
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.
9
int number; Float length=7
int number; Float length=7.5; Main() { ---- } Function1() Function2() -----
10
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.
11
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);
12
fun2() { int x; x=1; return(x); } Fun3() x=x+10; 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.
13
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.