Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c

What are storage class specifiers in C?

Umme Ammara

Every variable has two properties in C language that are:

  1. data type (int, char, float, etc.)
  2. Storage Class

The Storage Class of a variable decides its scope, lifetime, storage location, and default value.

A variable’s storage location is either the CPU memory or the memory registers.

Types of Storage Classes

In C, there are four storage classes. Any variable in C belongs to one of these classes, shown below:

Storage Class Specifiers

The storage class specifiers are the keywords that tell the compiler the variable’s storage class. These specifiers are declared, as shown below:

The four storage class specifiers are:

  • auto
  • static
  • extern
  • register

Suppose a storage class specifier is not declared. In that case, the variables inside the function body are auto by default, variables outside the function body are static, and the variables within a function are extern.

auto

A variable specified with the auto specifier belongs to the automatic storage class. This variable has the following properties:

Storage Location Lifetime Scope Initial Value
CPU Memory Within the function body Local Garbage Value

The variables defined within a function block are local to the function. The variable is not accessible outside the function block because it gets destroyed on the block’s exit.

static

A variable specified with the static specifier belongs to the static storage class. This variable has the following properties:

Storage Location Lifetime Scope Initial Value
CPU Memory Retains the variable’s value Local Zero

A static variable will retain its value in between different function calls.

register

A variable specified with the register specifier belongs to the register storage class. This variable has the following properties:

Storage Location Lifetime Scope Initial Value
Register Memory Within the function body Local Garbage Value

The variables defined within a function block are local to the function. They are not accessible outside the function, similar to the auto specifier. However, the register variable gets stored in register memory instead of CPU memory, providing faster access to the variables.

extern

A variable specified with the extern specifier belongs to the external storage class. This variable has the following properties:

Storage Location Lifetime Scope Initial Value
CPU Memory Till the end of the program file Global Zero

An extern variable is defined anywhere in the file and is accessible anywhere in the main source file or others.

A variable that is not initialized but has allocated memory stores a garbage value.

Example

The code below shows the declaration of storage class specifiers in C:

  1. The auto variable a is defined and printed in the main function.

  2. The register variable b is defined and printed in the test_func.

  3. The extern variable c is defined outside the functions and printed in the main function.

  4. The static variable d is defined, modified, and printed inside the main function.

#include <stdio.h>

//extern storage class specifier 
extern int c; 
int c = 30; 

int test_func() {
  // register storage class specifier 
  register int b = 20; 
  printf ("Value of b: %d\n", b);
}

int main() {

  //auto storage class specifier 
  auto int a = 10;  
  printf ("Value of a: %d\n", a);

  //Call the test_func 
  test_func();

  printf ("Value of c: %d\n", c);

  //static storage class specifier 
  static int d = 40;  
  d = d - 5;
  printf ("Value of d: %d\n", d);

  return 0;
}

RELATED TAGS

c

CONTRIBUTOR

Umme Ammara
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring