Tag Archives: c storage class

C storage class

Every variable declaration has two property associated with it. storage class and data type. storage class defines that where to store a variable.

 

Syntax : storage_class datatype variable_name;

 

There are following storage classes in C.

auto,  register,  static,   extern

S.No. Storage Specifier Storage Place
1 auto CPU Memory
2 extern CPU Memory
3 static CPU Memory
4 Register Register Memory

 

auto storage class is a default storage class for all local variable. auto storage class variable has local scope.

Both declaration of variable a and b are same. Default storage is taken as auto storage.

extern storage class is used to give reference to the global variable of current file or other files. extern storage variable cannot be initialized because it refers to the global variable of current or other file.

If we deal with multiple files and we want to use global variable or function of other file in current file then we can access the gloabl variable or function of other file using storage keyword and define the variable name same as in other files.

Example:

file: 1.c

file: 2.c

 

Compile: gcc 1.c 2.c

output: ./a.out

10

 

Static storage class is default storage class for global variable and static variables.Static variables are initialized only once.

static variable can be defined as below :

The below example will explains more about static variable.

Output :

1 2 3

 

register storage class is similar to local variable. The only difference is the value stored in microprocessor register instead of RAM. This class is faster than other classes. We should not define many register variables other wise the processing will go slower.

Register variable declaration.