Storage Classes
Storage Classes
Storage Classes
By S.K. Ansari
Lets Starts with Storage Classes
You already know about are previous method of variable declaration
auto
register
extern
static Int a = 10 ;
TYPE Name
Storage Class
● Default Value
● Location
● Scope
● Life time
Syntax to Declare var with SC
int add(void) {
int a=13;
auto int b=48;
return a+b;}
The auto Storage Class
● The auto storage class is the default storage class
for all local variables.
● The keyword auto is rarely used while writing
{ programs in C language.
● Auto variables can be only accessed within the
int mount; block/function they have been declared and not
outside them (which defines their scope).
auto int month; ● However, they can be accessed outside their scope as
well using the concept of pointers given here by
} pointing to the very exact memory location where
the variables reside.
● They are assigned a garbage value by default whenever
they are declared.
Take this example : =
The auto Storage Class
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
Output : = 3 2 1
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);}
The auto Storage Class
#include<stdio.h>
int main(){
int i; Output: Garbage Garbage Garbage
auto char c;
float f;
printf("%d %c %f",i,c,f);
return 0;
}
The auto Storage Class
#include<stdio.h>
int main(){
int a=10; Output: 20 10
{
int a=20;
printf("%d",a);
}
printf(" %d",a);
return 0;
}
#include<stdio.h>
int main(){
{
int a=20;
printf("%d",a); Output: Compilation error
}
printf(" %d",a); //a is
not visible here
return 0;
}
#include<stdio.h>
Question: What will be
int main(){
output of following c
int a=0;
code?
{
int a=10;
printf("%d",a);
a++;
{
a=20;
}
{
printf(" %d",a);
int a=30; {a++;}
printf(" %d",a++);
}
printf(" %d",a++);
}
printf(" %d",a);
return 0;}
Extern Storage Class
#include <stdio.h>
int main()
{
printf("%d", a);
return 0;
}
Extern Storage Class
int var;
int main(void)
{
var = 10;
return 0;
}
This program compiles successfully.
var is defined (and declared implicitly)
globally.
extern int var;
int main(void)
{
return 0;
}