hamburger

Storage Class in C

By BYJU'S Exam Prep

Updated on: September 25th, 2023

Every program written in C is highly dependent on variables. These are the most important aspects of C programming. In C, every variable has two properties: type and storage class in C. Among them, the type refers to the variable’s data type, and the storage class in C determines the variable’s scope, lifetime, and visibility. Storage class in C are used to determine a variable’s lifetime, visibility, memory location, and initial value.

In this article, we will take a close look at storage class in C, their types, and how their characteristics influence program output using some programming examples. In C, a storage class in C is used to represent variable information.

Download Formulas for GATE Computer Science Engineering – Programming & Data Structures

Storage Class in C

A variable storage class informs us of four things:

  • Where the variable might be stored.
  • The variable’s scope, or where in the program we can access the variable’s value.
  • The variable’s life, or how long the variable will be active in the program (longevity or alive).
  • If the variable is not initialized, it returns its default value.

Storage Class in C describes the characteristics of a variable/function. These features include the scope, visibility, and lifetime, which allow us to track the existence of a variable during the program’s execution.

Download Formulas for GATE Computer Science Engineering – Algorithms

Types of Storage Class in C

The C programming language employs four storage classes that are as follows:

  • Auto Storage Class
  • Extern Storage Class
  • Static Storage Class
  • Register Storage Class

Auto Storage Class in C

Auto storage class in C is the default storage class for all variables declared within a function or block. As a result, the keyword auto in the auto storage class in C is rarely used when writing C programs. Auto variables can only be accessed within the block/function in which they were declared, not outside of it (which defines their scope).

Auto Storage Class in C can also be accessed outside of their scope using the pointer concept described here by pointing to the exact memory location where the variables reside. By default, they are given a garbage value when they are declared.

The following is the syntax for defining an auto variable:

auto int a; / Declare explicitly

int b; / Auto by default

Let us look at an example,

#include

int main()
{

int *pointer; if(1)
{

int secondsPerMinute = 60;

pointer = &secondsPerMinute;

}

printf(\%d, *pointer);

}

Output:

60

The variable secondsPerMinute is local to the if block and can be accessed within the if block’s braces. However, it can be accessed by the pointer variable in the main function’s block because it is still alive (allocated memory) in the parent function and we can access its value outside the if block but inside the main function by dereferencing through the pointer variable.

Objects with the auto storage class are not accessible to the linker, which means that auto variables in other files cannot be used during the link-time.

Download Formulas for GATE Computer Science Engineering – Digital Logic

Extern Storage Class in C

The extern storage class in C indicates that the variable is defined somewhere other than the block used. Essentially, the value is assigned to it in another block and can be overwritten/changed in another block.

So an extern variable is a global variable that has been initialized with a legal value where it is declared to be used elsewhere. The extern storage class in C is accessible from any function/block. A regular global variable can also be made an extern storage class in C by inserting the ‘extern’ keyword before its declaration/definition in any function/block.

The following is the syntax for defining an extern variable:

extern int a; 

Let us look at an example,

main.C

#include

extern int count;

void change_global(void);

void main()

{

printf(Inside main:Initial value of count: %d,count);

change_global();

count=5;

change_global();

printf(Inside main:Final value of count: %d,count);

}

global.c

#include

int count;

void change_global(void)

{

printf(Inside global:Current value of count: %d,count);

count = count + 10;

printf(Inside global:Current value of count: %d,count);

}

Output:

Inside main:Initial value of count: 0

Inside global:Current value of count: 0

Inside global:Current value of count: 10

Inside global:Current value of count: 5

Inside global:Current value of count: 15

Inside main:Final value of count: 15

When these two files are independently compiled and linked, they function as a single file. Here, main.c contains a function and variable declarations of changeglobal and gVar, which are defined in file global.c; both files will be successfully compiled and linked at link-time.

Static Storage Class in C

Static storage class in C is used to declare static variables, commonly used when writing C programs. Static variables retain their value even after they have been removed from their scope! As a result, static variables retain the value of the most recent use within their scope.

The static storage class in C is only initialized once and exists until the program terminates. As a result, no new memory is allocated because they have not been re-declared. Their scope is restricted to the function they were created anywhere in the program. The compiler assigns them the value 0 by default.

The following is the syntax for defining an static variable:

static int a; 

Let us look at an example,

#include

static int out; // Global static variable implicitly initialized to 0.

void main()

{

static int in = 10;//local static variable explicitly ini to 10

printf(global: %d, local: %d, out, in);

}

Output:

global: 0, local: 10

Out is a global static variable with the default value 0 and in is a local static variable with a scope limited to the main function but a lifetime that extends until the end of the programme, which is shared by both global and local static variables.

Register Storage Class in C

The register storage class in C defines register variables with the same functionality as auto variables. The only difference is that the compiler tries to store these variables in a microprocessor register if a free registration is available. This makes the use of register variables much faster than those stored in memory during the program’s runtime.

Typically, a few variables that will be accessed frequently in a program are declared with the register keyword, which improves the program’s running time. An important and intriguing point to note here is that pointers cannot be used to obtain the address of a register variable.

The following is the syntax for defining an register variable:

register int a;

Let us look at an example,

#include

int main ()

{

int *pointerToLocal;

for (register int i = 0; i < 1000000; i++)

{

if(i==0)

pointerToLocal = &i;

}

printf(\%d,pointerToLocal);

}

Output:

register_storage.c: In function ‘main’:

register_storage.c:9:7: error: address of register variable ‘i’ requested pointerToLocal = &i;

Because of its frequent use in the for loop, defining a register variable inside a for loop may be acceptable by the compiler to allocate register instead of storing it in memory.

In this case, I is a register variable that has been initialised within a for loop, and pointerToLocal is a pointer variable that is attempting to get the address of I but the compiler throws an error saying that you cannot request the address of a register.

Feature of Storage Class in C

Storage class in C are essential in memory management in C programs. Use storage classes in your C program from now on to access the program’s memory. Don’t let the compiler intimidate you.

Storage class Storage Default Initial Value Scope Lifetime
Automatic Stack garbage local within block end of the block
Register Stack or CPU register garbage local within block end of the block
Static Static memory 0 Local or global, within a block or within the file End of the program
External Static memory 0 Global End of the program

As a result, the use of global and static variables should be kept to a minimum because they are alive until the programme terminates, consuming static memory throughout the program’s life beginning at compile time.

Advantages and Disadvantages of Storage Class in C

The advantage of the storage class in C is that the storage class in C assists in determining a variable’s scope, visibility, and lifetime. In the storage class in C, more than one variable with the same name can be used.

The disadvantage of the storage class in C is that in storage class in C, changing the program to meet different needs becomes difficult because we must consider the variable’s scope and lifetime. A single variable cannot have more than one storage class.

Important GATE Topics

Types Of Op Amp Tellegen’s Theorem
What Is Gear Degree Of Static Indeterminacy
Difference Between Linker And Loader Millman’s Theorem
OP Amp Simple Op Amp Circuits
Maxwells Reciprocal Theorem Difference Between Abstract Class And Interface
Our Apps Playstore
POPULAR EXAMS
SSC and Bank
Other Exams
GradeStack Learning Pvt. Ltd.Windsor IT Park, Tower - A, 2nd Floor, Sector 125, Noida, Uttar Pradesh 201303 help@byjusexamprep.com
Home Practice Test Series Premium