0
1.5kviews
What are different Storage Classes in C or Explain the Scope and Life Time of the Variables.
1 Answer
0
15views

A variable in C can have any one of the four storage classes.

  1. Automatic variables.
  2. External variables.
  3. Static variables.
  4. Register variables.

Automatic Variables (Local/Internal)

Automatic variables are declared inside a function in which they are to be utilized. They are created when a function is called and destroyed automatically when the function is exited.

Eg:

main() 
{ 
int number; 
}

We may also use the keyword auto to declare automatic variables explicitly.

External Variables

Variables that are both alive and active throughout the entire program are known as External variables. They are also known as global variables. Eg:

int number; 
float length = 7.5; 
main() 
{ 
} 
function1( ) 
{ 
} 
function2( ) 
{ 
}

The keyword extern can be used for explicit declarations of external variables.

Static Variables

As the name suggests, the value of a static variable persists until the end of the program. A variable can be declared static using the keyword static.

Eg:

1) static int x;

2) static int y;

Register Variables

We can tell the compiler that a variable should be kept in one of the machine’s registers, instead of keeping in the memory. Since a register access is much faster than a memory access, keeping the frequently accessed variables in the register will lead to faster execution of programs. This is done as follows:

register int count;

Please log in to add an answer.