0
6.3kviews
What is significance of storage class? Illustrate each storage class with example
1 Answer
0
182views

Significance of storage class:

  • Every C variable has a storage class and a scope.

  • The storage class determines the part of memory where storage is allocated for an object and how long the storage allocation continues to exist.

  • It also determines the scope which specifies the part of the program over which a variable name is visible, i.e. the variable is accessible by name.

  • It also determines life of the variable.

enter image description here

Automatic Storage class:

  • In this data is stored in memory.

  • Default value is garbage value.

  • Scope of variable is local to block.

  • Life is, with in the block in which the variable is defined.

  • Example: int i; or auto int i;

Program:

#include<stdio.h>
int main() { 
Increment();
Increment();
Increment();
}
Increment(){
auto int i=1; 
printf("%d",i);
i = i +1 
}

Output: 1 1 1

Register Storage Class:

  • In this data is stored in CPU register.

  • Default value is garbage value.

  • Scope of variable is local to block.

  • Life is, with in the block in which the variable is defined.

  • Example: register int i;

Program code:

#include<stdio.h>
int main() {
register int i=2;
printf("%d",i);
}

Output: 2

Static Storage Class:

  • In this data is stored in memory.

  • Default value is zero.

  • Scope of variable is local to block.

  • Life is, value of the variable persists between different functions calls.

  • Example: static int i;

Program:

#include<stdio.h>
#include<conio.h>

int main() { 
test(); 
test(); 
} 

int test() { 
static int i=10;
i = i + 1;
printf("\n%d",i); 
}

Output: 10 11

External Storage Class:

  • In this data is stored in memory.

  • Default value is zero.

  • Scope of variable is local to block.

  • Life is, as long as the program execution doesn’t come to an end.

Program:

#include<stdio.h>
int i=10; 
int main() { 
int i=2; 
printf("%d",i); 
display(); 
} 
display() { 
printf("\n%d",i);
}

Output: 2 10

Please log in to add an answer.