storage classes

15
STORAGE CLASSES http:// computerprogramminginclanguage.blogspot.com/

Upload: leela-koneru

Post on 09-Aug-2015

329 views

Category:

Education


76 download

TRANSCRIPT

Page 1: Storage classes

STORAGE CLASSES

http://computerprogramminginclanguage.blogspot.com/

Page 2: Storage classes

STORAGE CLASSES

Introduction Types of storage classes Scope, visibility and lifetime of storage

classes

Page 3: Storage classes

INTRODUCTION

To declare and define a variable we need to specify the datatype. But , to fully declare a variable, it is also important to specify the storage class.

If we don’t specify a storage class of a variable in it’s declaration, the compiler will assume a storage class depending on the context of the variable is used. i.e., each variable has a specified default storage class

Variables are generally stored in 2 locations Memory CPU registers

Page 4: Storage classes

INTRODUCTION CONTINUE…

By Using Storage class we come to know the following Where the variable would be stored. What will be the initial value of the variable, if

the initial value not specified then what is the default value.

What is the scope of the variable, i.e., in which functions the value of the variable would be available.

What is the life time of the variable, i.e., how long would be the variable exist.

Page 5: Storage classes

TYPES OF STORAGE CLASSES

Automatic Variables

External Variables

Static Variables

Register Variables

Page 6: Storage classes

AUTOMATIC VARIABLES Automatic variables are declared inside a

function(block) in which they are used. Keyword used to declare a automatic

variable is auto . Auto variables are stored in memory. Default initial values is garbage value. Scope is local to the block/function in which

they are declared. Lifetime of auto variable is till the end of the

function in which the variable is defined These are also referred as internal or local

variables

Page 7: Storage classes

AUTOMATIC VARIABLES CONTINUE…

void main() { auto int i; printf(“ i: %d”,i); }

Output: GarbageValue(Unexpected Value)

Page 8: Storage classes

EXTERNAL VARIABLES

External variables are declared outside all functions, so that they are available to all the functions.

Keyword used to declare external variable is extern.

Extern variables are stored in memory. Default initial value is zero. Scope is global Lifetime of extern variable is as long as the

program is executed. These are also referred as global variables

Page 9: Storage classes

EXTERAL VARIABLES CONTINUE…

int i; extern int i; void main() { printf(“ i: %d”,i); } Output: 0

Page 10: Storage classes

STATIC VARIABLES

Static variables can declared either internal or external .

Keyword used to declare static variable is static.

Extern variables are stored in memory. Default initial values is zero. Scope is global/local based on the

declaration. Lifetime of internal static variable is local to

the function or external static variables is as long as the program is executed.

Static variables can be initialized only once

Page 11: Storage classes

STATIC VARIABLES CONTINUE… void main() { void increment(); increment(); increment(); increment(); } void increment() { static int i=1; //int I or auto int I; printf(“ i: %d”,i); i++; } Output: i=1 i=2 i=3

Page 12: Storage classes

REGISTER VARIABLES

Register variables can declared inside a function .

Keyword used to declare static variable is register.

Extern variables are stored in CPU registers. Register access is much faster than a

memory access, by keeping frequently accessed variables like looping variable(iteration) in the register leads to faster execution of a program.

Page 13: Storage classes

REGISTER VARIABLES CONTINUE…

Default initial values is garbage value. Scope is local to the block . Lifetime of variable is with in the block.

Page 14: Storage classes

REGISTER VARIABLES CONTINUE…

void main() { register int i; for(i=1;i<=5;i++) printf(“ i: %d”,i); } Output: i:1 i:2 i:3 i:4 i:5 /* Not sure the variable is stored in register,

because it is limited*/

Page 15: Storage classes

Storage Class

Keyword

Storage

Default initial value

Scope Lifetime

Automatic

auto memory Garbage Local to the block

With in the block

External extern memory Zero Global Till the end of program exe

Static static memory Zero Local Value of the variable persists b/w diff. function calls

Register register CPU register

Garbage Local to the block

With in the block

OVERVIEW