Storage Class in C
Storage Class in C
WOMEN
(Autonomous) Afternoon Session Chennai 18.
S.I.E.T.
Prepared by
M. MINU MEERA, Assistant Professor
A. JUNAITHA BARVEEN, Assistant
Professor
Keyword
Where it is Declared
Storage Area
Default Initial value
Lifetime of a variable
void function1()
Keyword : auto {
Declaration : Inside the function int m=10;
Storage Area : Stack printf("%d\n",m);
}
Initial Value : Garbage value (At the
time of compilation compiler assigns any value) void function2()
Lifetime : Upto that function only {
int m=100;
function1();
Example : printf("%d\n",m);
auto int x; (or) int x; }
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 5
of Computer Science
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 6
of Computer Science
#include<stdio.h>
int k;
void function1();
void function2();
A variable which can be access with in a void function3();
void main()
function and outside the main function.
{
These variables are also named as Global k=20;
variables or External variables function1();
Keyword : extern function2();
Declaration : Outside of the main() function function3();
}
Storage Area : CPU–memory void function1() {
Initial Value : zero k=k+10;
printf("%d\n",k); }
Lifetime : Upto the entire program void function2()
Example : {
k=k+1000;
int x; printf("%d\n",k);
main() main() }
void function3()
{ { {
(or) extern int x; k=k+10;
printf("%d\n",k);
} } }
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department of Computer Science 7
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department 8
of Computer Science
/* To print the value of x */
#include<stdio.h>
void stat();
This variable static is constant and the value void main()
is continued in all the steps. {
Keyword : static int i;
Declaration : Inside the function for(i=1;i<=5;i++)
stat(); //calling
Storage Area : CPU – memory
}
Initial Value : Zero void stat() //definition
Lifetime : The value of the variable persists {
static int x=0;
between different function calls.
printf("x=%d\n",x);
Example : x=x+1;
static int x; }
0
1
2
3
4
5
06/22/24 Presenter Name: M.MINU MEERA ,A.JUNAITHA BARVEEN, Department
of Computer Science 9
/* Example 2 */
#include<stdio.h>
#include<conio.h>
void incre(); /* Function prototype declaration */
void main()
{
clrscr();
incre();
incre(); The character Stored in x is A
incre();
getch(); The character Stored in x is B
}
The character Stored in x is C
void incre()
{
static char x=65;
printf("\n The character stored in x is %c",x++);
}
Call by value
Call by reference
Three steps in using a function are defining a function, prviding a prototype and calling
the function.
Return statement is used to return the information from the function to the calling
Scope of a variable is defined as the region over which the variable is visible or valid.
3. Write a program to check whether the year is leap year or not using functions?
4. Write a program to find the square of first N Numbers and to calculate its sum?