Storage Classes In C Language

C Storage Class Definition

Storage class in C decides the part of storage to allocate memory for a variable, it also determines the scope of a variable.

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

The storage class of a variable in C determines the life time of the variable if this is 'global' or 'local'. Along with the life time of a variable, storage class also determines variable's storage location (memory or registers), the scope (visibility level) of the variable, and the initial value of the variable.

There are four types of storage classes in C

Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerRegisterGarbage ValueLocalWithin the function

Auto Storage

A variable defined within a function or block with auto specifier belongs to automatic storage class.

All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned.

A variables having automatic storage class are local to the block which they are defined in, and get destroyed on exit from the block.

Example:

Output:

 
 garbage garbage garbage 



Register Storage Class

The register specifier declares a variable of register storage class.

Variables belonging to register storage class are local to the block which they are defined in, and get destroyed on exit from the block.

The variables defined as the register is allocated the memory into the CPU registers depending upon the size of the memory remaining in the CPU.

We can store pointers into the register, i.e., a register can store the address of a variable.

Example 1:



Output:

  15

Example 2:



Output:

	//error: address of register variable requested


Static Storage Class

Static variables can be used within function or file. Unlike global variables, static variables are not visible outside their function or file, but they maintain their values between calls.

The variables defined as static specifier can hold their value between the multiple function calls.

A same static variable can be declared many times but can be assigned at only one time.

Default initial value of the static integral variable is 0 otherwise null.

Example:



Output:

  1 0
  2 1

A static global variable has internal linkage that means even though the variable is global; routines in other files have no knowledge of it and cannot access and alter its contents directly.



External Storage Class

The extern specifier gives the declared variable external storage class. The principal use of extern is to specify that a variable is declared with external linkage elsewhere in the program.

The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program.

If a variable is declared as external then the compiler searches for that variable to be initialized somewhere in the program which may be extern or static. If it is not, then the compiler will show an error.

Example 1:



Output:

  x: 10

Example 2:



Output:

  compile time error 
  main.c: In function ?main?:
  main.c:5:16: error: ?a? has both ?extern? and initializer
  extern int a = 0;