c Programming-chapter 09
c Programming-chapter 09
PRESENTED BY
SHEYONA G
CHAPTER-09
STORAGE CLASS
Function
In C a variable not only have data type but have storage classes also.
The storage class tells us about the scope, visibility and longevity of a variable.
Scope: It determines over what region of the program a variable is actually available for use.
Longevity (Life time): It is the duration of time in which a variable exists in the memory during execution.
Visibility: It is the program’s ability to access a variable from the memory
The four storage classes in “C” are:
• Automatic Variables.
• Register Variables.
• Static Variables.
• External Variables.
Automatic storage class
Automatic variables are declared inside a function in which they are used.
They are created when the function is called and destroyed automatically when the function is exited.
Automatic variables are local / private to the function in which they are declared.
Its also referred to as local or internal variables.
To declare automatic variables use the keyword “auto”.
Syntax:
auto datatype var1,var2…var n;
Ex: auto int x=20;
#include <stdio.h>
void main()
{
auto int i=1;
{
auto int i=2;
{
auto int i=3;
printf(“%d”,i);
}
printf(“%d”,i);
}
printf(“%d”,i);
}
Output: 3 2 1
Register storage class
In Register storage class value will be stored in CPU registers.
A value stored in CPU register can always be accessed faster than that is stored in memory.
While using register variable user cannot use scanf() function because values are stored in
CPU register not in memory.
Syntax:
register datatype v1,v2….vn;
Example:
#include<stdio.h>
void main()
{
register int a=10,b=20,c;
c=a+b;
printf("C=%d ",c);
}
Output: C=30
Static storage class
In static variable, the value of the variable does not change between the different
function calls.
Static variables are also local to the block in which it is defined.
The difference between automatic & static variable is that it does not disappear when
the function is no longer active. Their value remains constant
#include<stdio.h>
void fun();
void main()
{
Output:
fun();
c=1
fun();
c=2
fun();
c=3
}
void fun()
{
static int c;
c++;
printf("c=%d\n",c);
}
External storage class
Variables that are active throughout the entire program are known as external variables.
They are also known as global variables.
Global variables are accessed by any function in the program.
External variables are declared outside the function.
int number;
float length;
main()
{
int mark;
}
function1()
{
}
External Declaration
The variable can be declared anywhere in the program.
By using “extern” keyword user can access that variable in entire program.
This variable will be active in that entire file and also other files.
Syntax:
extern datatype v1,v2,…vn;
Example:
void fun1();
void main()
{
extern int y; Output: 5 6
printf("%d\n",y);
fun1();
}
int y=5;
void fun1()
{
extern int y;
y++;
printf("%d",y);
}
ASSIGNMENTS
Automatic variables are also called as ________________. external global internal register
machine
Register variables are stored in _______________. memory header file heap
register
Global variables are also called as ________________. external local internal register
Which of the following variable is initialized only once? register automatic static external
Explain the method of passing two-dimensional integer array to a function
List out the different types of storage class in C language.
What is an automatic variable?
Write a program to illustrate the properties of a local or automatic variable.
What is a register variable?
What is a static storage variable?
Write the syntax and example for declaring static and register variable.
What is a global variable?
Write the syntax and example for declaring internal and external variable.
Write a C program to illustrate the use of static variable.