1
SIRIFORT COLLEGE OF COMPUTER TECHNOLOGY AND
MANAGEMENT
A Presentation Report
On
Storage Classes In ‘C’
Submitted By:- Nitesh Bichwani
02224302013
B.C.A 1st semester
Evening shift
Submitted To:- mss. Aditi Sabharwal
(asst. prof.)
mss. Nidhi Gupta
(asst. prof.)
Submitted as a part of general proficiency for the degree of
BACHALORS‘ OF COMPUTER APPLICATION.
Language ‘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.
In 1983, the American National Standards Institute (ANSI)
established a committee to provide a modern ,
comprehensive definition of C.
The resulting definition, the ANSI standard, or "ANSI C",
was completed late 1988.
 It’s a case-sensitive language in which lower and upper
case has an individual role.
Dennis Ritchie
(1941-2011)
4
Storage classes in ‘c’
Each and every variable has a storage type which is to defined at the time of type
declaration.
we are able to make programs without giving its storage type because it is taken as
default.
 the storage class of a variable tells following information:-
1. The location where the variable can be stored.
2. The default value that the class take when the value of a variable is not assign.
3. Scope of the variable in which the value of that variable will be available.
4. Life of the variable is that for how much time will it remain on the memory.
5
Types of Storage Classes
in ‘c’
Automatic
Storage
Class
Register
Storage
Class
Static
Storage
Class
External
Storage
Class
6
Automatic storage class
7
Storage :- Memory
Default initial value :- Garbage value (45223,-548796) etc.
Scope :- Local to the block where the variable is defined.
Life :- Till the execution of block.
Declared with keyword auto
 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.
 Only local variables and parameters can be of automatic storage class.
• Such variables normally are of automatic storage class.
8
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
garbage value.
9
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
The value of I in 1st block will be 79 in 2nd block
will be 89 and in 3rd block will be 99.
10
Output Screen
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.
Register Storage Class
11
Storage :- CPU (Registers)
Default initial value :- Garbage value (45223,-548796) etc.
Scope :- Local to the block where the variable is defined.
Life :- Till the execution of block.
Declared with keyword register.
 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.
 It is faster in use than automatic. As it is stored in registers.
12
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
register int i;
printf("%d n",i);
getch();
}
Output Screen
Conclusion:-
This shows that in Static storage class without defining a variable it gives
a garbage value.
13
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
The value of I in 1st block will be 79 in 2nd block will be
89 and in 3rd block will be 99.
14
Output Screen
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.
Scope says that it is valid in the block of function where it is defined.
Static Storage Class
15
Storage :- Memory
Default initial value :- Zero (0)
Scope :- Local to the block where the variable is defined.
Life :- value persist among different function calls.
Declared with keyword Static
 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.
 If we call a function twice and do increment or decrement then the value
remains same till the end of program.
Persists:- To continue in existence.
16
Example 1
#include<stdio.h>
#include<conio.h>
void main()
{
static int i;
printf("%d n",i);
getch();
}
Output Screen
Conclusion:-
This shows that in Static storage class without defining a variable it gives “0”
as a value.
17
Example 2
#include<stdio.h>
#include<conio.h>
void function();
void main()
{
function();
function();
function();
getch();
}
void function()
{
static int i=1;
printf("%d n",i);
i++;
}
Execution :-
After entering into main
First time function called.
“i” is declared 1
“i” is printed
“i” is incremented to 2
Get back to main
Again function called
Now 13 statement will not be executed
“i” will be printed as 2
“i” is incremented to 3
Get back to main
Again function called
Now again 13 statement will not be executed
“i” will be printed as 3
“i” is incremented to 4
Get back to main
Main end
Statement 13
18
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.
Output Screen
External Storage Class
19
Storage :- Memory
Default initial value :- Zero (0)
Scope :- Global
Life :- As long as the program doesn’t come to an end.
Declared with keyword Extern
 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.
20
Example 1
#include<stdio.h>
#include<conio.h>
int x=12;
void main()
{
extern int y;
printf("%d %d n",x,y);
getch();
}
int y=13;
Output Screen
Conclusion:-
This shows that in external storage class a variable can be define outside the
function if earlier it I declared by keyword extern.
Declaration
Definition
21
Example 2
#include<stdio.h>
#include<conio.h>
int x=50;
extern int y;
void display();
void function();
void main()
{
printf("%d n",x);
display();
function();
getch();
}
void display()
{
printf("%d n",y);
function();
}
void function()
{
printf("%d n",y);
}
int y=90;
2 different
functions are
called 3 times in
total. But the
value remains
same.
Declaration
Definition
22
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.
23
Difference between the four with the help of an example.
Bases Automatic
Storage Class
Register
Storage Class
Static Storage
Class
External
Storage Class
Storage Memory CPU registers Memory Memory
Default Initial
Value
Garbage Value Garbage Value Zero (0) Zero (0)
Scope Local to the
block in which
the variable is
defined.
Local to the
block in which
the variable is
defined.
Local to the
block in which
the variable is
defined.
Global
Life Till the control
remains within
the block in
which the
variable is
defined.
Till the control
remains within
the block in
which the
variable is
defined.
Value of the
variable
persists
between
different
function calls.
As long as the
programs
execution
doesn’t come to
an end.
Thank You
Presented by:-
Nitesh Bichwani
Abhishek Bansal
24

Storage classes in C

  • 1.
    1 SIRIFORT COLLEGE OFCOMPUTER TECHNOLOGY AND MANAGEMENT A Presentation Report On Storage Classes In ‘C’ Submitted By:- Nitesh Bichwani 02224302013 B.C.A 1st semester Evening shift Submitted To:- mss. Aditi Sabharwal (asst. prof.) mss. Nidhi Gupta (asst. prof.) Submitted as a part of general proficiency for the degree of BACHALORS‘ OF COMPUTER APPLICATION.
  • 2.
  • 3.
    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
  • 4.
    About Language ‘C’ TheC programming language was designed by Dennis Ritchie at Bell Laboratories in the early 1970s. In 1983, the American National Standards Institute (ANSI) established a committee to provide a modern , comprehensive definition of C. The resulting definition, the ANSI standard, or "ANSI C", was completed late 1988.  It’s a case-sensitive language in which lower and upper case has an individual role. Dennis Ritchie (1941-2011) 4
  • 5.
    Storage classes in‘c’ Each and every variable has a storage type which is to defined at the time of type declaration. we are able to make programs without giving its storage type because it is taken as default.  the storage class of a variable tells following information:- 1. The location where the variable can be stored. 2. The default value that the class take when the value of a variable is not assign. 3. Scope of the variable in which the value of that variable will be available. 4. Life of the variable is that for how much time will it remain on the memory. 5
  • 6.
    Types of StorageClasses in ‘c’ Automatic Storage Class Register Storage Class Static Storage Class External Storage Class 6
  • 7.
    Automatic storage class 7 Storage:- Memory Default initial value :- Garbage value (45223,-548796) etc. Scope :- Local to the block where the variable is defined. Life :- Till the execution of block. Declared with keyword auto  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.  Only local variables and parameters can be of automatic storage class. • Such variables normally are of automatic storage class.
  • 8.
    8 Example 1 #include<stdio.h> #include<conio.h> void main() { autoint i; printf("i=%d n",i); getch(); } Output screen Conclusion:- This shows that in automatic storage class without defining a variable it gives a garbage value.
  • 9.
    9 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 The value of I in 1st block will be 79 in 2nd block will be 89 and in 3rd block will be 99.
  • 10.
    10 Output Screen Conclusion:-  Inthis 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.
  • 11.
    Register Storage Class 11 Storage:- CPU (Registers) Default initial value :- Garbage value (45223,-548796) etc. Scope :- Local to the block where the variable is defined. Life :- Till the execution of block. Declared with keyword register.  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.  It is faster in use than automatic. As it is stored in registers.
  • 12.
    12 Example 1 #include<stdio.h> #include<conio.h> void main() { registerint i; printf("%d n",i); getch(); } Output Screen Conclusion:- This shows that in Static storage class without defining a variable it gives a garbage value.
  • 13.
    13 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 The value of I in 1st block will be 79 in 2nd block will be 89 and in 3rd block will be 99.
  • 14.
    14 Output Screen Conclusion:- This exampleis 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. Scope says that it is valid in the block of function where it is defined.
  • 15.
    Static Storage Class 15 Storage:- Memory Default initial value :- Zero (0) Scope :- Local to the block where the variable is defined. Life :- value persist among different function calls. Declared with keyword Static  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.  If we call a function twice and do increment or decrement then the value remains same till the end of program. Persists:- To continue in existence.
  • 16.
    16 Example 1 #include<stdio.h> #include<conio.h> void main() { staticint i; printf("%d n",i); getch(); } Output Screen Conclusion:- This shows that in Static storage class without defining a variable it gives “0” as a value.
  • 17.
    17 Example 2 #include<stdio.h> #include<conio.h> void function(); voidmain() { function(); function(); function(); getch(); } void function() { static int i=1; printf("%d n",i); i++; } Execution :- After entering into main First time function called. “i” is declared 1 “i” is printed “i” is incremented to 2 Get back to main Again function called Now 13 statement will not be executed “i” will be printed as 2 “i” is incremented to 3 Get back to main Again function called Now again 13 statement will not be executed “i” will be printed as 3 “i” is incremented to 4 Get back to main Main end Statement 13
  • 18.
    18 Conclusion:- In this examplethe 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. Output Screen
  • 19.
    External Storage Class 19 Storage:- Memory Default initial value :- Zero (0) Scope :- Global Life :- As long as the program doesn’t come to an end. Declared with keyword Extern  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.
  • 20.
    20 Example 1 #include<stdio.h> #include<conio.h> int x=12; voidmain() { extern int y; printf("%d %d n",x,y); getch(); } int y=13; Output Screen Conclusion:- This shows that in external storage class a variable can be define outside the function if earlier it I declared by keyword extern. Declaration Definition
  • 21.
    21 Example 2 #include<stdio.h> #include<conio.h> int x=50; externint y; void display(); void function(); void main() { printf("%d n",x); display(); function(); getch(); } void display() { printf("%d n",y); function(); } void function() { printf("%d n",y); } int y=90; 2 different functions are called 3 times in total. But the value remains same. Declaration Definition
  • 22.
    22 Output Screen Conclusion:- This showsthat 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.
  • 23.
    23 Difference between thefour with the help of an example. Bases Automatic Storage Class Register Storage Class Static Storage Class External Storage Class Storage Memory CPU registers Memory Memory Default Initial Value Garbage Value Garbage Value Zero (0) Zero (0) Scope Local to the block in which the variable is defined. Local to the block in which the variable is defined. Local to the block in which the variable is defined. Global Life Till the control remains within the block in which the variable is defined. Till the control remains within the block in which the variable is defined. Value of the variable persists between different function calls. As long as the programs execution doesn’t come to an end.
  • 24.
    Thank You Presented by:- NiteshBichwani Abhishek Bansal 24