Storage Class
What is a Storage Class?
Decides the part of storage to allocate memory for a variable
Determines the scope of a variable.
All variables defined in a C program get some physical location in
memory where variable's value is stored.
Memory and CPU registers are types of memory locations where a
variable's value can be stored.
The storage class of a variable in C determines the life time of the
variable if this is 'global' or 'local'.
Along with the life time of a variable, storage class also determines
variable's storage location (memory or registers), the scope (visibility
level) of the variable, and the initial value of the variable.
Storage Class Types
Storage Class Types
Automatic Storage Class
A variable defined within a function or block
with auto specifier belongs to automatic
storage class.
All variables defined within a function or block
by default belong to automatic storage class if
no storage class is mentioned.
Variables having automatic storage class are
local to the block which they are defined in, and
get destroyed on exit from the block.
Automatic Storage Class -
Example
#include <stdio.h>
int main( )
{
auto int i = 1;
{
auto int i = 2;
{
auto int i = 3;
printf ( "\n%d ", i);
}
printf ( "%d ", i);
}
printf( "%d\n", i);
}
OUTPUT
321
Register Storage Class
The register specifier declares a variable of register storage class.
Variables belonging to register storage class are local to the block which they are
defined in, and get destroyed on exit from the block.
A register declaration is equivalent to an auto declaration, but hints that the
declared variable will be accessed frequently; therefore they are placed in CPU
registers, not in memory.
Only a few variables are actually placed into registers, and only certain types are
eligible; the restrictions are implementation-dependent.
Register variables are also given no initial value by the compiler.
Register Storage Class - Example
#include<stdio.h>
int 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);
return(0);
}
Static Storage Class
The static specifier gives the declared variable static storage
class.
Static variables can be used within function or file.
Unlike global variables, static variables are not visible outside
their function or file, but they maintain their values between
calls.
The static specifier has different effects upon local and global
variables.
Static Storage Class - Example
#include <stdio.h>
void staticDemo() int main()
{ {
int a=1; staticDemo();
staticDemo();
{ }
static int i = 1;
printf("%d ", i);
i++;
}
printf("%d\n",a);
a++;
}
External Storage Class
The extern specifier gives the declared variable external storage class.
The principal use of extern is to specify that a variable is declared with external
linkage elsewhere in the program.
To understand why this is important, it is necessary to understand the difference
between a declaration and a definition.
A declaration declares the name and type of a variable or function.
A definition causes storage to be allocated for the variable or the body of the
function to be defined.
The same variable or function may have many declarations, but there can be only
one definition for that variable or function.
When extern specifier is used with a variable declaration then no storage is
allocated to that variable and it is assumed that the variable has already been
defined elsewhere in the program.
When we use extern specifier the variable cannot be initialized because with
extern specifier variable is declared, not defined.
Extern Variable
Extern Variable - Example
Problem when extern is not used
int main()
{
a = 10; //Error: cannot find definition of variable 'a'
printf("%d", a);
}
Example using extern in same file
int main()
{
extern int x; //informs the compiler that it is defined somewhere else
x = 10;
printf("%d", x);
}
int x; //Global variable x
Extern Variable - Example
#include <stdio.h>
extern int x;
int main()
{
printf("x: %d\n", x);
}
int x = 10;
Extern Variable - example
File1.c
#include <stdio.h>
int i=10;
void fun()
{
i++;
printf("%d",i);
}
File2.c
# include"file1.c";
main()
{
extern int i;
fun();
}
Output: 11
Extern Variable
• Example 4:
#include "somefile.h"
extern int var;
int main(void)
{
var = 10;
return 0;
} - Supposing that somefile.h has the definition of var. This program will be compiled successfully.
• Example 5:
extern int var = 0;
int main(void)
{
var = 10;
return 0;
} - If a variable is only declared and an initializer is also provided with that declaration, then the
memory for that variable will be allocated i.e. that variable will be considered as defined.
Therefore, as per the C standard, this program will compile successfully and work.