Sessio 67 - StorageClasses

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

STORAGE CLASSES

Session-6-7

KLEF P-1(CTD) BES-1


Session Outcome
Session Outcome: At the end of this session on Storage Classes
– Introduction, Types, and Examples.

 Students will be able to:

 Solve computer applications using structured programming.

 Think in logical way to solve real-time applications using


storage classes.

KLEF P-1(CTD) BES-1


Objective
The storage class will determine
 Where the variable will be allocated memory
( in register or main memory etc...)
 Lifetime of the variable
 how long the variable stays in memory
(life-time )
 Scope of the variable
 the part of C program in which the variable is
accessible or visible.
 Whether the variable will be automatically
initialized or not.
KLEF P-1(CTD) BES-1
Why storage Class?
 Each variable has a storage type which is to be
defined at the time of type declaration.
 We can write programs without giving its
storage type because it is taken as default.
 The storage class defines scope (visibility) and
lifetime of variable and functions declared
within a C program.

KLEF P-1(CTD) BES-1


Storage class in C

 Determines memory allocation of variable

 Lifetime of a variable in memory

 Scope of a variable

KLEF P-1(CTD) BES-1


Storage Classes in ‘C’
C supports 4 storage classes.
 automatic
 static
 register
 external

KLEF P-1(CTD) BES-1


Variable
variable

Internal (Local) External(Global)


int a; Declaration
main() External variable
{
main() }
{
int a;

}
Declaration
Internal variable

KLEF P-1(CTD) BES-1


Automatic storage class
Variable created when program enters its block

Variable destroyed when program leaves block

All local variables of functions belong to


automatic storage class by default

Automatic variables are allocated memory in


stack block of program memory.

keyword auto explicitly declares automatic


KLEF P-1(CTD) BES-1
Automatic storage class
Summary
Keyword auto
Accessibility Within function or block in
which it is declared
Storage Main memory
Existence Exists in memory as long as
the control is within the block
or function in which it is
declared. Once the control
returns from function or block,
it is removed from memory
Default Value Garbage

KLEF P-1(CTD) BES-1


Example of Automatic Storage Class
#include<stdio.h>
#include<conio.h> This variable scope and lifetime is within
void main() main method
{
auto int i=1;
{
auto int i=2;
This variable {
scope and lifetime auto int i=3;
is within this printf(“\n%d”,i);
block }
printf(“%d”,i);
}
printf(“%d”,i);
getch();
}

Output: 3 2 1

KLEF P-1(CTD) BES-1


Static Storage Class
 Variable exists in memory till the program
ends
 retains value between function calls
 Default Storage class for global variables
 Stored in Global or static block of program
memory

KLEF P-1(CTD) BES-1


Static Storage Class
Summary
Keyword static
Accessibility Local: Within function or block in which it
is declared
Global: Within the program in which it is
declared
Storage Main memory
Existence Local: Exists through out the execution of
the program
Global: Exists through out the execution of
the program files
Default Value zero

KLEF P-1(CTD) BES-1


Dif. b/w auto and static storage class
Block diagram function name- increment
Input: initialize static int i=1;
main() method Output: value of i
void increment(void)

function call
Call print value of i;
increment() thrice
increment() increment i by 1;
with no arguments

program terminates after After function call been


execution of three function Controls returns to executed
calls main method

KLEF P-1(CTD) BES-1


Example for static variable
#include<stdio.h>
void increment();
void main()
{ Output:
increment(); 1 2 3
increment();
increment();
}
This statement executes ONLY when the
void increment() increment() function is called for the first
{ time.
The variable i exists in memory till the
static int i=1;
program ends
printf("%d\t",i);
i++;
}

KLEF P-1(CTD) BES-1


Dif. b/w auto and static storage class
Block diagram function name- increment
Input:initialize auto int i=1;
main() method Output: value of i
void increment(void)

function call
Call print value of i;
increment() thrice
increment() increment i by 1;
with no arguments

program terminates after After function call been


execution of three function Controls returns to executed
calls main method

KLEF P-1(CTD) BES-1


Dif. b/w auto and static storage class
#include<stdio.h>
void increment();
void main()
{ Output:
increment(); 1 1 1
increment();
increment();
}
void increment()
{
auto int i=1;
printf("%d\t",i);
i=i+1;
}
KLEF P-1(CTD) BES-1
Block Diagram
Function name:fun
Input: initialize static int i=5;
int j=5;
Output: i and j
main() method void function- fun(void)

Call fun() method twice


function call with
no arguments increment i and j by 1;
print value of i and j

Program terminates Controls returns


to main method

KLEF P-1(CTD) BES-1


Predict the output
#include <stdio.h>
void func(void);
int main()
{
func();
func(); Output:
return 0;
} i is 6 and j is 6
void func( )
{ i is 7 and j is 6
static int i = 5;
int j=5;
i=i+1;
j=j+1;
printf("i is %d and j is %d\n", i, j);
}

You might also like