In this article Storage Classes in C we give the information about a storage class defines the scope and lifetime of variables and functions. Basically it is a keyword which is used before the declaration of variable or function.
Storage Classes in C:
This keyword changes the way that variable or function works.
Storage Classes decides the scope and lifetime of Variables.
It tells where to store the Storage Classes Variables. for eg. CPU, Register.
Types of Storage Classes:
There are four types of Storage Classes.
- Automatic Storage Class
- External Storage Class
- Register Storage Class
- Static Storage Class
Storage Classes in C:
Automatic Storage Class:
The auto storage class of C is considered the default class for all local variables. Whenever you create a local variable and do not define any other storage class with it, then by default (automatically) it belongs to the auto storage class.
- In Automatic Storage Class ‘auto’ keyword is used.
- It is just like a normal variable.
- This is a local variable.
- Their visibility or scope is within the function.
- They get destroyed outside.
- Their default value is ‘garbage’.
Syntax for Automatic Storage Class:
auto data_type variable_name = value(optional);
for eg.
int a ; // and
auto int a ; // both are same
Source Code:
#include <stdio.h>
int main()
{
auto int a;
auto int b = 15;
printf(“Value of a : %d\n”, a);
printf(“Value of b : %d”, b);
return 0;
}
Output :
Value of a : 1568 // garbage value
Value of b : 15
External Storage Class:
Many times it happens that you are working on a big project, and then you have to handle more than one program files. In such a situation, external storage class is very helpful. Suppose you have declared a variable in a program file.
- This variable is a global variable.
- Now if you want to use this variable in any other program file of the same project, then you use extern keyword.
- ‘external’ keyword is used in External Storage Class.
- The scope of variables of External Storage Class is Global.
- Due to global variables, they are used anywhere in the program including extern and inside any function.
- Their default value is ‘0’.
Syntax for External Storage Class
extern data_type variable_name = value(optional);
for eg.
extern int a ;
Source Code:
#include <stdio.h>
int num = 15 ;
void func(); // function declaration
int main()
{
extern int num ;
printf(“Value of num : %d\n”,num);
func(); // function calling
return 0;
}
void func()
{
// function definition
extern int num ;
printf(“Value of num : %d”,num);
}
Output :
Value of num : 15
Value of num : 15
Storage Classes in C:
Register Storage Class:
A register is a part of the computer processor that holds small data. But it can hold only very small values. It is many times faster than RAM. Therefore, wherever you need fast data access in your program, you can use this storage class.
- ‘Register’ keyword is used in Register Storage Class.
- The scope of variables of Register Storage Class is Local.
- Due to Local Variable, their use remains visible inside the function in which they are declared or initialized in the program.
- Register Storage Variables are stored on the register of the computer.
- Memory of Register Storage Class is ‘Limited’. If the memory of the register gets exhausted then it is stored on the CPU memory.
- Register variables do not have any address(&).Their default value is ‘garbage’.
Syntax for Register Storage Class
register data_type variable_name = value(optional);
for eg.
register int a ;
Source Code:
#include <stdio.h>
int main()
{
register int num = 15 ;
printf(“Value of num : %d\n”,num);
// printf(“Value of num : %d\n”,&num);
return 0;
}
Output :
Value of num : 15
C Static Storage Class:
Static storage class tells the compiler that the variable will remain consistent till the end of the program. Here by consistent I mean that the variable will not be created and destroyed again and again. Suppose you have created a variable inside the function. Whenever you call this function, this variable is created and it gets destroyed as soon as the function ends.
That is, if you make any changes in that variable by calling the function, then even those changes will not remain after the function exits. When you call the function again, the variable will be created with its initial value. But if you use the static keyword, then the variable will be created only once in the entire program and will be destroyed at the end of the program. And whenever you change the value of the variable, those changes will also remain during the entire
- In Static Storage Class, ‘static’ keyword is used.
- Variables of Static Storage Class have both Local and Global scope.
- Their default value is ‘0’.
Syntax for Static Storage Class
static data_type variable_name = value(optional);
for eg.
static int a ;
program.
#include <stdio.h>
void numFunction();
int main()
{
numFunction();
numFunction();
}
void numFunction
{
/* Static variable num */
static int num=0;
num=num+2;
printf(“%dt”,num);
}
In the above program it does not matter how many times you are calling numFunction(). Whenever you call it, this function will use the value of num variable changed by the old function call.
2 4
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining