Storage Class
STORAGE CLASSES
● Storage classes are used to define the scope (visibility) and life-time of
variables and/or functions
● Every variable and function in C has two attributes: type and storage class.
● There are four storage classes:
• auto
• static
• extern or global
• register
THE AUTO STORAGE CLASS :
The auto keyword is applied to all local variables automatically. It is the default storage
class that is why it is known as automatic variable.
EXAMPLE PROBLEM:
#include<stdio.h>
int main()
{
int a=10;
auto int b=10;//same like above
printf("%d %d",a,b);
return 0;
}
Output:
10 10
The register storage class :
● Is used to define local variables that should be stored in a register instead of RAM
● Variable has a maximum size equal to the register size (usually one word)
● Can't have the unary '&' operator applied to it (as it does not have a memory
location).
{
register int miles;
}
● The register should only be used for variables that require quick access such as
counters.
● Defining 'register' does not mean that the variable will be stored in a register
● Means that it might be stored in a register depending on hardware and
implementation restrictions.
The Static Storage Class :
● Instructs the compiler to keep a local variable in existence during the life-time of the
program instead of creating and destroying it each time it comes into and goes out of
scope
● Making local variables static allows them to maintain their values between function
calls
● The static modifier may also be applied to global variables and it causes that
variable's scope to be restricted to the file in which it is declared
● When static is used on a global variable, it causes only one copy of that member to be
shared by all the objects of its class
The Static Storage Class (contd.,):
The static variable has the default value 0 which is provided by compiler.
#include<stdio.h>
int func()
{
static int i=0;//static variable
int j=0;//local variable
i++;
j++;
printf("i= %d and j= %d\n", i, j);
}
int main()
{
func();
func();
func();
return 0;
}
Output:
i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1
EXTERN STORAGE CLASS:
● is used to give a reference of a global variable that is visible to ALL the program
files
● When Extern is used the variable cannot be initialized however, it points the
variable name at a storage location that has been previously defined
When you have multiple files and you define a global variable or function, which
will also be used in other files, then extern will be used in another file to provide
the reference of defined variable or function. Just for understanding, extern is
used to declare a global variable or function in another file.
The extern modifier is most commonly used when there are two or more files sharing the
same global variables or functions as explained below.
First File: main.c
#include <stdio.h>
int count ;
extern void write_extern();
main()
{
count = 5;
write_extern();
}
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void) {
printf("count is %d\n", count);
}
Comments
Post a Comment