Professional Documents
Culture Documents
Storage class
Storage class
Auto variables are defined inside a function. A variable declared inside the function without
storage class name, by default is an auto variable. These functions are declared on the stack. The
keyword auto can be used to declare the auto variables.
Example: auto int a;
#include <stdio.h>
#include <conio.h>
int main()
{
int a; //auto
char b;
float c;
printf("%d %c %f",a,b,c); // printing initial default value of automatic variables a, b, and
c.
return 0;
}
Output:
2. External Variables
Page- 2
The variables that are available to all the functions in the program i.e., from entire program they
can be accessed are called an external variable or a global variable. External variables are declared
outside of the function body. The keyboard extern can be used to declare an external variable.
Example:
extern int a;
Program:
#include<stdio.h>
#include<conio.h>
int x=10;
int f1();
int f2();
void main()
{
printf("\n result %d",x);
f1();
f2();
getch();
}
int f1()
{
int x=20;
printf("\n %d",x);
}
int f2()
{
printf("\n %d",x);
}
Output:
3. Static Variables
If a variable is defined with the keyword static then it is called static storage class variable. The
initial value of static storage class variable is zero. We can also declare the static variable both the local
and global.
The static is mainly used to maintain the previous value of the variable if the programming is
there or not. Generally static is used for counting the function how many times it will invoke.
Program:
#include<stdio.h>
void sum();
void main()
{
int i;
printf("%d %d ",a,b);
sum();
}
void sum(); // The static variables holds their value between multiple function calls.
{
static int a = 10;
static int b = 24;
printf("%d %d ",a,b);
a++;
b++;
}
Output:
4. Register Variables
If a variable is declared with the keyword register then it is called as register storage class
variable. When a variable is defined under the register then the value of the register variable directly
stored in CPU registers.
As we know the processor fastly access the registers, so that the register storage class variables
are fastly accessed compare to normal variables.
Float is not suitable for register class variable .Initial value is garbage, scope and life time is
within the block.
The following program illustrates the working of register storage class.
Aim: To write a C program that illustrates the use of a register variable in C.
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
register int a;
a=10;
printf("\n %d",a);
getch();
}
Output: