Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

STORAGE CLASSES IN C

 STORAGE CLASSES are used to determine the LIFETIME, VISIBILITY, MEMORY


LOCATION, and INITIAL VALUE of a variable.

ex. int x;

Storage class tells us:


1) WHERE the variable is stored. (Location of x)
2) INITIAL VALUE of the variable. ( initial value of x)
3) SCOPE OF THE VARIABLE. Scope specifies the part of the program which a
variable x is accessed. (visibility of x)
4) LIFE OF THE VARIABLE How long variable exist in the program.(Lifetime of x)
STORAGE CLASSES IN C

There are FOUR types of storage classes in C


1. Automatic (auto)
2. Static (static)
3. External (extern)
4. Register (register)

Storage Storage Default Value Scope Lifetime


Classes Place

auto RAM Garbage Value Local Within function

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

register Register Garbage Value Local Within the function


STORAGE CLASSES IN C- AUTO STORAGE CLASS

 Automatic variables or storage classes are #include<stdio.h>


ALLOCATED MEMORY AUTOMATICALLY AT
int main()
RUNTIME.
{
 The variable is declared with keyword auto. auto int x;
printf(“%d”, x);
 (auto keyword is optional while declaring
return 0;
variables.)
 They must be declared at the start of a block }
 Memory is allocated automatically upon entry
to a block and freed automatically upon exit Output
from the block.
 The automatic variables are initialized to 0 (Garbage Value)
garbage by default.
STORAGE CLASSES IN C- AUTO STORAGE CLASS

 Storage- stored in memory #include <stdio.h>


int main()
 Default initial value- garbage value
{
(int - 0, float-0.000000, char- null) int a; //auto
 Scope- local to the block in which they are char b;
declared , including any blocks nested within float c;
that block. For this reasons automatic variables printf("%d %c%f",a,b,c);
return 0;
are also called local variable.
}
 Life- within the block in which the variable is
Output:
defined 0 0.000000 (garbage)
STORAGE CLASSES IN C- STATIC STORAGE CLASS

 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()

count++; count++; count++;


count=1 count=2 count=2
STORAGE CLASSES IN C- STATIC STORAGE CLASS

 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

Q) What is the output of the following program


#include <stdio.h>
int fun()
{
static int num = 16;
return num--;
}

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

 allocated the memory into the CPU registers depending upon


Ex.
the size of the memory remaining in the CPU.
#include <stdio.h>
 we can not use &operator for the register variable.
 The access time of the register variables is faster than the
int main()
automatic variables.
{
 The initial default = 0. register int a;
 The “register” keyword is used for the variable which should printf("%d",a);
be stored in the CPU register.. }
 We can store pointers into the register, i.e., a register can Output:
store the address of a variable. 0
 Static variables can not be stored into the register since we
can not use more than one storage specifier for the same
variable.
STORAGE CLASSES IN C- EXTERN STORAGE CLASS

 Tell the compiler that the variable defined as


extern is declared with an external linkage
elsewhere in the program.
 Extern storage class is used when we have global
functions or variables which are shared between
two or more files.
 The default initial value of external integral type
is 0 otherwise null.
 Keyword extern is used to declaring a global
variable or function in another file to provide the
reference of variable or function which have
been already defined in the original file
STORAGE CLASSES IN C- EXTERN STORAGE CLASS

First File: main.c


#include <stdio.h>
extern i;
int main()
{
printf("value external integer is = %d\n",
i);
return 0;
}

Second File: original.c


#include <stdio.h>
int i=48;

You might also like