Storage Classes

 

Storage classes in c

Storage classes tell us about three things.
1.     Storage classes tell us about the memory. It means a variable uses which type of memory. It uses RAM or any other memory (CPU Memory).
2.     Storage classes tells us about the scope of variable. The scope of a variable is defined as the area of program in which it is accessible.
3.     Storage classes tells us about the default value of the variable.
There are four types of storage classes.
1. Auto
2. Static
3. Extern
4. Register
 1. Auto: - Auto means automatically. It is the default scope of variables and they are declared with or without auto keyword, belong to auto storage class. Auto variable are active in the block in which they are declared. Block means statements inside the braces ({}).
a)     The scope of auto variable is limited. It means they are local variable. They can access within the block where they are declared.
b)    The auto storage class variable uses primary memory for storing data. It means RAM.
c)     The default value of auto storage class is garbage.


For Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a,b; int c;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&a,&b);
c = a + b;
printf("Sum=%d",c);
getch();
}
Note: - As shown above a,b and c all are belong to the auto storage class.


2.     Static variable: - Static variable are those variable, which start with static keyword. Static variables retain their values through out the execution of the program. Their initial value is zero. Static variable also use primary memory for storing data. Static variables are also active in the block in which they are declared, means the scope of variable is local. The declaration part of static variable execute only once even if we call function more than once. Static variables are mostly used in functions.
As shown in example: -
#include <stdio.h>
#include<conio.h>
 void show();
 void main()
{
clrscr();
show();
show();
show();
                        getch();
}
void show()
{
static int a;
printf("\n%d",a);
a=a+1;
}
Outout:-
0
1
2

3. Extern variable: - Extern variable are those variables, which are, declared outside of the block/main. This variable can be accessible by each block of the program. They are known as global variables. These variables are declared with extern keyword. Its Initial value is zero. They also use a primary memory (RAM) for storing a data.They consider as local variable but work as global variables.

E.g :
#include<stdio.h>
#include<conio.h>
void show();
int a;
void main()
{
extern int a;
 clrscr();
printf("\n the value of a=%d”,a);
show();
a=a+10;
printf("\nNow the value of a=%d",a);
getch();
}
void show()
{
extern int a;
a=a + 5;
printf("The value of a= %d”,a);
}
Outout:-
0
5
15

4.     Register variable: -Register variable are those variable, which Start with register keyword. We can use register keyword to increase the speed of program. The Resister variable uses the CPU memory. Their default value is garbage. They are also block level variables, which can access with in the block where they are declared. In case of non-availability of CPU memory, these variables use primary memory (RAM) 
Ex.
#include<stdio.h>
#include<conio.h>
void main()
{
register int a=5;
 clrscr();
 printf("The value of a=%d",a);
 getch();
}




Difference between auto variable and a static variable

auto variable
Static variable
Auto variables are start with or without auto keyword.

Static variable start with static keyword.
They cannot retain their value throughout the execution of the program.
They retains their value throughout the execution of the program
Their default value is garbage
It's default value is zero.
These variable initialize every time when we call the function.
These variable initialize a single time and retain their value.

Auto int a;
Static int a;

Important Long Answer Type Questions
1. Explain the scope of variables in C?.
2. Give the difference between local and global variables in C?.
3. Explain the storage classes in C? Discuss their scope and use.
4.Discuss in brief, the difference between the following declarations.
a. static int i;b. auto int i;














0 comments:

Post a Comment