Professional Documents
Culture Documents
Storage Classes(1)
Storage Classes(1)
ex. int x;
extern RAM Zero Global Till the end of the main program Maybe declared anywhere
in the program
static RAM Zero Local Till the end of the main program, Retains value between
multiple functions call
The static variable is a variable that is capable of retaining its value between multiple numbers of function calls in a
program
Count is initialized once count=0
static int After calling f1() count=1
count=0 After calling f2() count=2
After calling f2() count=3
f3()
f1() f2()
The variables defined as static specifier can HOLD THEIR // STATIC STORAGE CLASS
VALUE BETWEEN THE MULTIPLE FUNCTION CALLS. #include<stdio.h>
int fun()
Static local variables are visible only to the function or the {
block in which they are defined. static int count = 0; //Initialized only once
count++; //shared among multiple function
static char c; return count;
static int i; }
int main()
{
printf("%d ", fun()); //count=1
printf("%d ", fun()); //count=2
return 0;
}
Storage- stored in memory Output:
Default initial value- 0
Scope- local to the block in which they are declared , 12
Life- Till the end of the main program, Retains value between multiple functions call
STORAGE CLASSES IN C- STATIC STORAGE CLASS
In C programming, when static is used on a global variable, it causes only one copy of that member to be
shared by all the functions
#include<stdio.h>
static int a = 10;
static int b = 24;
void sum()
{
printf("%d %d \n",a,b);
a++;
b++;
}
void main()
{
int i; Output:
for(i = 0; i< 3; i++)
{
10 24
sum(); .
} 11 25
} 12 26
STORAGE CLASSES IN C- STATIC STORAGE CLASS
int main()
{
for(fun(); fun(); fun())
printf("%d ", fun());
return 0;
}
A. Infinite loop
B. 13 10 7 4 1
C. 14 11 8 5 2
D. 15 12 8 5 2
STORAGE CLASSES IN C- STATIC STORAGE CLASS
//STATIC STORAGE CLASS fun() called first time: num = 16 // for loop initialization done;
In test condition, compiler checks for non zero value
#include <stdio.h>
int fun() fun() called again : num = 15
{
printf("%d \n", fun());:num=14 ->printed
static int num = 16;
return num - - ; Increment/decrement condition check
}
fun(); called again : num = 13
----------------
int main()
{ fun() called second time: num: 13
for(fun(); fun(); fun())
In test condition,compiler checks for non zero value
printf("%d ", fun());
return 0; fun() called again : num = 12
}
A. Infinite loop printf("%d \n", fun());:num=11 ->printed
B. 13 10 7 4 1 fun(); called again : num = 10
C. 14 11 8 5 2
D. 15 12 8 5 2
STORAGE CLASSES IN C- STATIC STORAGE CLASS
//STATIC STORAGE CLASS
#include <stdio.h>
int main()
{
static int i=5;
if(--i){
main();
printf("%d ",i);
}
return 0;
}
a) 4 3 2 1
b) 1 2 3 4
c) 0 0 0 0
d) Compiler Erro2
STORAGE CLASSES IN C- STATIC STORAGE CLASS
//STATIC STORAGE CLASS
#include <stdio.h>
int main()
{
static int i=5; A static variable is shared among all calls of a function. All calls
if(--i){ to main() in the given program share the same i. i becomes 0
main(); before the printf() statement in all calls to main().
printf("%d ",i);
}
return 0;
}
a) 4 3 2 1
b) 1 2 3 4
c) 0 0 0 0
d) Compiler Erro2
STORAGE CLASSES IN C- STATIC STORAGE CLASS
#include <stdio.h>
int main()
{
static int i=5;
if (- -i){
printf("%d ",i);
main();
}
}
a) 4 3 2 1
b) 1 2 3 4
c) 4 4 4 4
d) 0 0 0 0
STORAGE CLASSES IN C- STATIC STORAGE CLASS
#include <stdio.h>
int main()
{
Question Explanation:
static int i=5;
Since i is static variable, it is shared among all calls to main().
if (- -i){
So is reduced by 1 by every function call.
printf("%d ",i);
main();
}
}
a) 4 3 2 1
b) 1 2 3 4
c) 4 4 4 4
d) 0 0 0 0
What is the output of the following program?
#include <stdio.h>
int main()
{
static int a = 3;
printf(“%d”, a --);
return 0;
}
a) 0
b) 1
c) 2
d) 3
Q) What is the output of the following program?
#include <stdio.h>
int main()
{
static int a = 3;
printf(“%d”, a --);
return 0;
}
a) 0
b) 1
c) 2
d) 3
STORAGE CLASSES IN C- REGISTER STORAGE CLASS