Sei sulla pagina 1di 24

SIRIFORT COLLEGE OF COMPUTER TECHNOLOGY AND

MANAGEMENT

A Presentation Report
On
Storage Classes In ‘C’

Submitted as a part of general proficiency for the degree of


BACHALORS‘ OF COMPUTER APPLICATION.

Submitted To:-1 mss. Aditi Sabharwal Submitted By:- Nitesh Bichwani


(asst. prof.) 02224302013
mss. Nidhi Gupta B.C.A 1st semester
(asst. prof.) Evening shift
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 Dennis Ritchie


case has an individual role. (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 Register Static External


Storage Storage Storage Storage
Class Class Class Class
6
Automatic storage class
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.
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

The value of I in 1st block will be 79 in 2nd block 9


will be 89 and in 3rd block will be 99.
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.
10
Register Storage Class
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. 11

 It is faster in use than automatic. As it is stored in registers.


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
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

The value of I in 1st block will be 79 in 2nd block will be 13


89 and in 3rd block will be 99.
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.


14
Static Storage Class
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. 15

Persists:- To continue in existence.


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.
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

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.

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

Storage Memory CPU registers Memory Memory

Default Initial Garbage Value Garbage Value Zero (0) Zero (0)
Value

Scope Local to the Local to the Local to the Global


block in which block in which block in which
the variable is the variable is the variable is
defined. defined. defined.

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

Potrebbero piacerti anche