Storage Classes in C: Different Types of Storage Classes [With Examples]

Coding with C is highly centered upon using variables in every program. Those are the key aspects of C programming. Every variable in C has two properties; type and storage classes. Among them, the type refers to the data type of the variable, and storage classes in C determine the scope, lifetime, and visibility of the variable.

In this blog post, we will have a detailed look at the storage classes in C, its types, and how its characteristics influence the output of the program with some programming examples.

What Are Storage Classes In C?

Storage classes in C allocate the storage area of a variable that will be retained in the memory. They are stored in the RAM of a system. Apart from the storage space, they determine the scope of a variable. Variables in the C program are stored in a physical location in the random memory, which is mainly the device’s memory and CPU registers.

Storage classes in C also define the lifetime of the variable and term it as ‘local’ or ‘global’. Storage classes are also useful to define the scope or visibility, and the initial value of the variable. There are primarily four storage classes in C, viz. automaticregisterstatic, and external. We will discuss each one by one further.

How Storage Classes In C Are declared?

The four storage classes in C are declared in a block or program with the storage class specifiers, auto, register, extern, static. There is one more storage class specifier, ‘typedef’ used in the syntactic form, and does not reserve storage. The specifiers instruct the compiler on storing the variables.

There is a key difference between defining and declaring a variable. Defining a variable is about allocating the memory for the variable and declaring it means initializing it with a value.

Syntax:

storage_class_specifier data_type variable_name;

Read: Interesting Project Ideas & Topics in C# For Beginners

Special Case: When no storage class specifier is declared or defined in a program

There is at least one storage class specifier is given in the variable declaration. But, in case, there is no storage class specifier specified, the following rules are followed:

1. Variables declared within a function are considered as auto.

2. Functions declared within a function are considered as an extern.

3. Variables and functions declared outside a function are considered static, with external linkage.

What are the Types of Storage Classes in C?

There are four storage classes in C, let’s have a look at them: 

1. Automatic Storage Classes

Every variable defined in a function or block belongs to automatic storage class by default if there is no storage class mentioned. The variables of a function or block belong to the automatic storage class are declared with the auto specifier. Variables under auto in C are local to the block where they are defined and get discarded outside the block.

A Simple Program Showing Automatic Storage Classes:

#include <stdio.h>

int main( )

{

auto int i = 11;

{

auto int i = 22;

{

auto int i = 33;

printf ( “%d “, i);

}

printf ( “%d “, i);

}

printf( “%d”, i);

}

The output of the Program:

3 2 1

Explanation:

In the above program, there is three times the variable i is declared. Variables with the same name can be defined in different blocks. Thus, this program will compile and execute successfully without any error. The function ‘printf’ in the innermost block will print 3 and the variable i in this block will be destroyed after the block ends.

The next one, the second outer block prints 2 which is then succeeded by the outer block which prints 1. The automatic variables are initialized properly; else you will get undefined values as the compiler does not give them an initial value.

2. Register Storage Classes

The variables belonging to a register storage class are equivalent to auto in C but are stored in CPU registers and not in the memory, hence the name. They are the ones accessed frequently. The register specifier is used to declare the variable of the register storage class. Variables of a register storage class are local to the block where they are defined and destroyed when the block ends.

A Simple Program Showing Register Storage Classes:

#include <stdio.h>

int main()

{

register int i = 10;

int *p = &i; //error: address of register variable requested

printf(“Value of i: %d”, *p);

printf(“Address of i: %u”, p);

}

Explanation:

In the above program, the code tries to get the address of variable i into the pointer variable p but as i is declared as a register variable, the code won’t compile and will display the error ” Error: address of register variable requested”. 

Only certain types of variables are placed into registers. Register variables are not given an initial value by the compiler.

Learn: C++ Vs Java: Difference Between C++ & Java

3. Static Storage Classes

The visibility of static variables is zero outside their function or file, but their values are maintained between calls. The variables with static storage class are declared with the static specifier. Static variables are within a function or file. The static specifier works differently with local and global variables.

Simple Programs Showing Static Storage Classes with Local and Global Variables:

i. Local Variable

#include <stdio.h>

void staticDemo()

{

 static int i;

{

 static int i = 1;

  printf(“%d “, i);

i++;

}

  printf(“%d”, i);

  i++;

}

int main()

{

  staticDemo();

  staticDemo();

}

The output of the Program:

1 0

2 1 

Explanation:

When a local variable is defined by a static specifier, inside a function or block, permanent storage space is created in the compiler. The static local variable is visible to the function or block where it is specified and retains its value between the function calls. In the above program, the static variable i is defined at two places in two blocks inside the staticDemo()function. staticDemo() is called two in the main function. In the next call, the static variables retain their old values and need not be initialized again.

ii. Global Variable

#include <stdio.h>

static int gInt = 1;

static void staticDemo()

{

  static int i;

  printf(“%d “, i);

  i++;

  printf(“%d”, globalInt);

  globalInt++;

}

int main()

{

  staticDemo();

  staticDemo();

}

The output of the Program:

0 1

1 2

Explanation:

Static variables need to be initialized only once in a program and they are retained throughout the lifetime. They have a default initial value of zero.

When a global variable or function is defined by a static specifier, then that variable or function is known only to the file in which it is defined. For a global variable, other file routines cannot access and alter its contents as a static global variable has internal linkage. In the above program, the static global variable globalInt and a static function staticDemo(), are defined as static and they cannot be used outside the C file.

4. External Storage Classes

External storage class variables or functions are declared by the ‘extern’ specifier. When a variable is declared with extern specifier, no storage is allotted to the variable and it is assumed that it has been already defined elsewhere in the program. With an extern specifier, the variable is not initialized. The reason why extern is used to specify a variable in a program to declare it with external linkage. 

A Simple Program Showing External Storage Classes:

#include <stdio.h>

extern int i;

int main()

{

printf(“i: %d”, i);

}

int i = 1;

Explanation:

In the above C program, if extern int i is removed, there will be an error “Undeclared identifier ‘i’ because the variable i is defined after being used in printf. The extern specifier instructs the compiler that variable i has been defined and is declared here.

If you change extern int i; to extern int i = 5; you will get an error “Redefinition of ‘i'” because the extern specifier does not initialize a variable.

Also Read: Top 7 Exciting Project ideas in C For Beginners

Final Words

This article details the concept of storage classes in C and tells you how its types differ from each other. When to use a particular storage class depends on the assignment and the scope, lifetime, and visibility of the variable you are dealing with.

If you are interested to learn more and need mentorship from industry experts, check out upGrad & IIIT Banglore’s PG Diploma in Full-Stack Software Development.

Prepare for a Career of the Future

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
Enroll Today

Leave a comment

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

×