Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Page- 1

Q). STORAGE CLASSES

The storage class of a variable tells to the compiler


 The storage area of the variable.
 The initial value of the variable if not initialized.
 The scope of the variable.
 Life of the variable i.e., how long the variable would be active in the program.
Any variable declared in c can have any one of the four storage classes.
1. Automatic variables.
2. External variables.
3. Static variables.
4. Register variables.

1. Automatic variables (auto storage classe):

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;

For an auto variable,

 The storage location is stack (RAM).


 The default value is a garbage value.
 The scope of the variable is local to block in which they are defined.
 The life time is up to the end of the block in which the variable is declared

The following program illustrates the working of auto variables in C.

Aim: C-Program to illustrates the working of auto variables in C.


Program:

#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;

For an external variable,

 The storage location is stack (RAM).


 The default value is zero (0).
 The scope of the variable is entire the program.
 The life time is end of the main() function.

The following program illustrates the working of an external storage class.


Aim: To illustrates the working of an external variable in c.

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.

The following program illustrates the working of a static variable.

Aim: To write a C program that illustrates the working of a static variable in C.


Page- 3

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:

You might also like