Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
MANAGEMENT
A Presentation Report
On
Storage Classes In ‘C’
2
CONTENTS
About Language ‘C’
Storage Classes In ‘C’
Types Of Storage Classes In ‘C’
Automatic Storage Class
Examples
Register Storage Class
Examples
Static Storage Class
Examples
External Storage Class
Examples 3
About Language ‘C’
The C programming language was designed by Dennis
Ritchie at Bell Laboratories in the early 1970s.
we are able to make programs without giving its storage type because it is taken as
default.
5
Types of Storage Classes
in ‘c’
Automatic variables
• Created when program execution enters block in which they are defined
• Exist while the block is active
• Destroyed when the program exits the block.
7
Only local variables and parameters can be of automatic storage class.
• Such variables normally are of automatic storage class.
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
auto int i;
printf("i=%d \n",i);
getch();
}
Output screen
Conclusion:-
This shows that in automatic storage class without defining a variable it gives a 8
garbage value.
Example 2
#include<stdio.h>
#include<conio.h>
void main()
{ // starting of main
auto int i=79;
{ // starting of block 1
auto int i=89;
{ // starting of block 2
auto int i=99;
{ // starting of block 3
printf("%d \n",i);
} // end of block 3
} // end of block 2
printf("%d \n",i);
} // end of block 1
printf("%d \n",i);
getch();
} // end of main
Conclusion:-
In this example the storage is done in memory and hence we can declare
“i” many times in several blocks.
The value of “i” changes in every block as the 1st block carry the value
which is different from the other block .
Here “i” has three different values and no error has occurred because of the
scope of the automatic storage class.
Scope says that it is only valid in the block of function where it is defined.
10
Register Storage Class
Storage :- CPU (Registers)
register variables
• Created when program execution enters block in which they are defined
• Exist while the block is active
• Destroyed when the program exits the block.
If the register in CPU has no space than the storage is done in memory. 11
Output Screen
Conclusion:-
This shows that in Static storage class without defining a variable it gives
12
a garbage value.
Example 2
#include<stdio.h>
#include<conio.h>
void main()
{ // starting of main
register int i=79;
{ // starting of block 1
register int i=89;
{ // starting of block 2
register int i=99;
{ // starting of block 3
printf("%d \n",i);
} // end of block 3
} // end of block 2
printf("%d \n",i);
} // end of block 1
printf("%d \n",i);
getch();
} // end of main
Conclusion:-
This example is as same as the automatic one but the only difference is that
this is stored in CPU register and automatic was stored in memory.
The value of “i” changes in every block as the 1st block carry the value
which is different from the other block .
Here “i” has three different values and no error has occurred because of the
scope of the registers storage class.
Static variables
• The value persists between the function calls
• They had the same value which they were having before.
• Exist from the point at which the program begins execution.
Output Screen
Conclusion:-
This shows that in Static storage class without defining a variable it gives “0”
as a value.
16
Example 2
#include<stdio.h> Execution :-
#include<conio.h> After entering into main
void function(); First time function called.
void main() “i” is declared 1
{ “i” is printed
function(); “i” is incremented to 2
function(); Get back to main
function(); Again function called
getch(); Now 13 statement will not be executed
} “i” will be printed as 2
void function() “i” is incremented to 3
{ Get back to main
static int i=1; Again function called
printf("%d \n",i); Now again 13 statement will not be executed
i++; “i” will be printed as 3
} “i” is incremented to 4
Get back to main
Main end
Statement 13 17
Output Screen
Conclusion:-
In this example the function is called 3 times and all the time value will be
different because in the definition of function there is a increment in which
value get stored and then again when it executed then the value get
incremented.
18
External Storage Class
Storage :- Memory
Scope :- Global
External variables
• The variable can be declare out side the functions.
• The variables of this class can be referred to as 'global or external variables.
19
Example 1
#include<stdio.h>
#include<conio.h> Declaration
int x=12;
void main()
{
extern int y;
printf("%d %d \n",x,y); Definition
getch();
}
int y=13;
Output Screen
Conclusion:-
This shows that in external storage class a variable can be define outside the
20
function if earlier it I declared by keyword extern.
Example 2
#include<stdio.h>
#include<conio.h>
int x=50;
extern int y; Declaration
void display();
void function();
void main()
{
printf("%d \n",x); 2 different
display();
function(); functions are
getch(); called 3 times in
}
void display() total. But the
{ value remains
printf("%d \n",y);
function(); same.
}
void function()
{
printf("%d \n",y);
}
int y=90; Definition 21
Output Screen
Conclusion:-
This shows that when an extern is used out side the function then whenever
it is called in any function then it will return the same value as its scope is
global it works same globally in the whole program.
22
Difference between the four with the help of an example.
Bases Automatic Register Static Storage External
Storage Class Storage Class Class Storage Class
Default Initial Garbage Value Garbage Value Zero (0) Zero (0)
Value
Life Till the control Till the control Value of the As long as the
remains within remains within variable programs
the block in the block in persists execution
which the which the between doesn’t come to
23
variable is variable is different an end.
defined. defined. function calls.
Thank You
24
Presented by:-
Nitesh Bichwani
Abhishek Bansal