24
1 SIRIFORT COLLEGE OF COMPUTER TECHNOLOGY AND MANAGEMENT A Presentation Report On Storage Classes In ‘C’ Submitted By:- Nitesh Bichwani 02224302013 B.C.A 1 st semester Submitted To:- mss. Aditi Sabharwal (asst. prof.) mss. Nidhi Gupta Submitted as a part of general proficiency for the degree of BACHALORS‘ OF COMPUTER APPLICATION.

Storage classes in C

Embed Size (px)

Citation preview

Page 1: Storage classes in C

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.

Page 2: Storage classes in C

2

Language ‘c’

Page 3: Storage classes in C

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

Page 4: Storage classes in C

4

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)

Page 5: Storage classes in C

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.

Page 6: Storage classes in C

6

Types of Storage Classes in ‘c’

Automatic Storage Class

Register Storage Class

Static Storage Class

External Storage Class

Page 7: Storage classes in C

7

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.

Only local variables and parameters can be of automatic storage class.• Such variables normally are of automatic storage class.

Page 8: Storage classes in C

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.

Page 9: Storage classes in C

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.

Page 10: Storage classes in C

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.

Page 11: Storage classes in C

11

Register Storage ClassStorage :- 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.

Page 12: Storage classes in C

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.

Page 13: Storage classes in C

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 mainThe value of I in 1st block will be 79 in 2nd block will be 89 and in 3rd block will be 99.

Page 14: Storage classes in C

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.

Page 15: Storage classes in C

15

Static Storage ClassStorage :- 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.

Page 16: Storage classes in C

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.

Page 17: Storage classes in C

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 2Get back to main Again function calledNow 13 statement will not be executed “i” will be printed as 2“i” is incremented to 3Get back to main Again function calledNow again 13 statement will not be executed “i” will be printed as 3“i” is incremented to 4Get back to main Main end

Statement 13

Page 18: Storage classes in C

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

Page 19: Storage classes in C

19

External Storage ClassStorage :- 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.

Page 20: Storage classes in C

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

Page 21: Storage classes in C

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

Page 22: Storage classes in C

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.

Page 23: Storage classes in C

23

Difference between the four with the help of an example.Bases Automatic

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

Page 24: Storage classes in C

24

Thank You

Presented by:- Nitesh Bichwani Abhishek Bansal