Storage Classes in C


In C language, the features of variables and functions are described by the storage classes like visibility and scope of q variable or function.

There are four types of storage classes in C language: Automatic variables, External variables, Static variables, and Register variables.

auto

Auto storage class is the default storage class for all the local variables. It is created when function is called. When the execution of function is completed, variables are destroyed automatically.

They are also known as local variables because they are local to a function. By default, they are assigned the garbage value by the compiler.

Scope − auto variables are local variables to the function block.

Default value − Garbage value is the default initialized value.

Lifetime − Lifetime of auto variable is bound by the block in which it is defined.

Here is an example of auto variable in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   auto int a = 28;
   int b = 8;
   printf("The value of auto variable : %d
", a);    printf("The sun of auto variable & integer variable : %d", (a+b));    return 0; }

Output

The value of auto variable : 28
The sun of auto variable & integer variable : 36

extern

External variables are also known as global variables. These variables are defined outside the function. These variables are available globally throughout the function execution. The value of global variables can be modified by the functions.

Scope − They are not bound by any function. They are everywhere in the program i.e. global.

Default value − Default initialized value of global variables are Zero.

Lifetime − Till the end of the execution of the program.

Here is an example of extern variable in C language,

Example

 Live Demo

#include <stdio.h>
extern int x = 32;
int b = 8;
int main() {
   auto int a = 28;
   extern int b;
   printf("The value of auto variable : %d
", a);    printf("The value of extern variables x and b : %d,%d
",x,b);    x = 15;    printf("The value of modified extern variable x : %d
",x);    return 0; }

Output

The value of auto variable : 28
The value of extern variables x and b : 32,8
The value of modified extern variable x : 15

static

Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function.

Scope − They are local to the block.

Default value − Default initialized value is Zero.

Lifetime − Till the end of the execution of the program.

Here is an example of static variable in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   auto int a = -28;
   static int b = 8;
   printf("The value of auto variable : %d
", a);    printf("The value of static variable b : %d
",b);    if(a!=0)    printf("The sum of static variable and auto variable : %d
",(b+a));    return 0; }

Output

The value of auto variable : -28
The value of static variable b : 8
The sum of static variable and auto variable : -20

register

Register variables tell the compiler to store the variable in CPU register instead of memory. Frequently used variables are kept in registers and they have faster accessibility. We can never get the addresses of these variables.

Scope − They are local to the function.

Default value − Default initialized value is the garbage value..

Lifetime − Till the end of the execution of the block in which it is defined.

Here is an example of register variable in C language,

Example

 Live Demo

#include <stdio.h>
int main() {
   register char x = 'S';
   register int a = 10;
   auto int b = 8;
   printf("The value of register variable b : %c
",x);    printf("The sum of auto and register variable : %d",(a+b));    return 0; }

Output

The value of register variable b : S
The sum of auto and register variable : 18

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 24-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements