Storage classes in a C Programming Language are classified into four main types according to the storage area and their behavior, in this c programming tutorial we use these storage classes in many programs:-
- Automatic storage class
- Static storage class
- Register
- Extern
Automatic storage class:
Automatic storage class variables in C Programming Language will be created and destroyed automatically. In the Automatic storage class, variables are stored in the stack area of the data segment or processor register.
Features:
Storage Memory
Scope Local / Block Scope
Lifetime Exists as long as Control remains in the block
Static storage class:
Static storage class variables in c language will only exist once and throughout the c program, this storage variable will be always active. Static storage class variables are stored in a static area of a data segment.
In C programming, there are four types of scopes available such as
- Body scope
- Function scope
- Program scope
- File scope
Static Storage Class:
The value of the static variable lingers until the execution of the whole c program. A static variable can be declared using a static keyword as shown below:
Syntax:
static int i;
Example:
#include <stdio.h>
void Check(); //prototype
void main()
{
Check(); //function calling
Check(); //function calling
Check(); //function calling
getch();
}
void Check(){
static int c=0;
printf(“c=%d\n”,c);
c+=10;
}
Register storage class:
The keyword ‘register’ in C programming language is used to define a local variable.
Syntax
register int count;
Example
#include<stdio.h>
void main()
{
int num1,num2;
register int sum;
printf(“\nEnter the Number 1 : “);
scanf(“%d”,&num1);
printf(“\nEnter the Number 2 : “);
scanf(“%d”,&num2);
sum = num1 + num2;
printf(“\nSum of Numbers : %d”,sum);
getch();
}
Extern: Global variable
Example:
#include<stdio.h>
void f1();
extern int i=10; //global variable
void main()
{
clrscr();
printf(“inside main=%d”,i);
f1();
getch();
}
void f1()
{
printf(“\noutside main method=%d”,i);
}