STORAGE CLASS IN C

Storage Class  In C Programming Language defines :

  • Memory to allocate for a variable.
  • Scope of the variable
  • Lifetime of a variable

Note : All variables defined in a C program get some physical location in memory where variable’s value is stored.

There are 4 storage classes defined in C Programming Language

 

1. static

Static variable is visible only inside their own function

  • It retains its values during the function calls.
  • It can be declared by adding static keyword before data type.
static data_type variable_name;

2.auto

An automatic variable are a variable which is declared inside a function or block.

  • By default, all the variables are automatic variable.
  • We can also declare an automatic variable manually by adding keyword auto.
auto data_type variable_name;

 

3.register

In order to store the value of the variable in register, we have to add a keyword “register” before the variable name.

register data_type variable_name;
  • Variable stored in register reduced the time for performing any operation on register variable.
  • It is visible only within the function.

Note : Declaring a variable doesn’t ensure that the variable will be stored in the registered it is just a request to a compiler to store that variable in register whereas it depends on register whether it saves the variable in register or not

4.extern

External variables can be used across multiple files.

Declaration of an external variable is done by adding a keyword ‘extern” before the variable name.

extern data_type variable_name;

 Storage for an extern variable is done only by initializing it.

Note : The extern keyword before the variable only tells the compiler about the name and data type of variable without allocating any storage for it.

However, if you initialize that variable, then it also allocates storage for the extern variable.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *