c
  1. c-storage-classes

C Storage Classes

C programming language provides four storage classes that define the scope and lifetime of the variables.

  1. auto
  2. static
  3. register
  4. extern

Syntax for Variable Declaration

storage_class data_type variable_name;

Example

#include <stdio.h>

void func_auto() {
  auto int a = 10;
  printf("Value of a using auto storage class : %d\n", a);  
}

void func_static() {
  static int a = 20;
  printf("Value of a using static storage class : %d\n", a);  
  a++;
}

int main() {
  func_auto();
  func_static();
  func_static();
  return 0;
}

Output

Value of a using auto storage class : 10
Value of a using static storage class : 20
Value of a using static storage class : 21

Explanation

  • auto storage class is used to represent local variables. It is an automatic default storage class, and it is created automatically when a variable is declared inside a function or block.
  • static storage class is used to declare local/global variables that are accessible throughout the program. If a variable is declared with the static keyword, it retains its value throughout the program's execution.
  • register storage is used to store variables in the CPU registers rather than in memory locations. It is not compulsory for a compiler to place a variable in the CPU register even if it is defined with a register keyword.
  • extern storage class is used for defining variables or functions in the main memory of the computer. It allows variables and functions to be shared between different C files.

Use

  • auto storage class is the default storage class used for declaring local variables. It is mostly used for function variables where we don't want to keep the value of the variable after the function call.
  • static storage class is used when we want to keep the value of a variable local, but also want to retain its value throughout the program's execution.
  • register storage class is used for faster access to variables. It should be used only if there is a significant need to speed up the variable access. The use of the register keyword is just a suggestion to the compiler for introducing a particular optimization.
  • extern storage class is used when we want to share variables or functions between different files.

Important Points

  • C provides four storage class keywords, namely, auto, static, register, and extern.
  • Variables with auto storage class are created when the program enters a block where they are declared and are destroyed when the block exits.
  • Variables with static storage class are created once when the program starts and last throughout its execution.
  • Variables with register storage class are stored in CPU registers for faster access.
  • Variables declared in one file and required in another file are declared with the extern storage class.

Summary

In C programming, Storage Classes are used to specify the lifetime and visibility of variables and functions. The four storage classes are auto, static, register, and extern. Understanding the use cases and differences between these storage classes is fundamental to writing optimized, efficient, and scalable code.

Published on: