What Is Storage Class In C?

What Is Storage Classes In C?
What Is Storage Classes In C?

Introduction

Have you ever wondered how the memory in computers works?

Or how the variables are stored in the memory?

There are some types of storage classes available in every programming language. So today, we will discuss Storage Classes in C.

The variables declared in C programs are different from other languages. We can use the same variable names in the C program in separate blocks. When we declare a variable, it is available only to a specific part or block of the program. The remaining block or other function cannot get access to the variable. The area or block of the C program from where the variable can be accessed is known as the variable’s scope.

There are four scope rules in C:

  • Function: Function scope begins at the opening of the function and ends with its closing. The function has local variables, Global variables, and formal parameters.
  • File: These variables are usually declared outside of all the functions and blocks at the top of the program and can be accessed from any portion of the program.
  • Block: Scope of an Identifier begins at the opening of the block / ‘{‘ and ends at the end of the block / ‘}.’ Identifiers with block scope are local to their block.
  • Function Prototype: Identifiers declared in the function prototype are visible within the prototype. The scope of these variables begins right after the declaration in the function prototype and runs to the end of the Declarations list. These scopes don’t include the function definition but just the function prototype.

Now we have just a rough idea about the variable’s scope, but how is this related to storage classes? Let’s explore them.

What are Storage Classes?

A storage class of variable tells us four things:

  • Where the variable would be stored.
  • The Scope of the variable, i.e., in which region of the program the variable’s value is available for us.
  • Life of the variable, i.e., how long the variable would be active in the program(longevity or alive).
  • The initial value of the variable, if it is not initialised.

Storage Classes are used to describe the features of a variable/function. A storage class defines the scope (visibility) and a lifetime of variables or functions within a C Program. These help to trace the existence of a particular variable during the runtime of a program. They precede the type that they modify.

We have four different storage classes in a C program:

  • auto
  • register
  • static
  • extern

Let’s see each storage class briefly and understand it better using different code snippets.

Automatic Variables

Automatic variables are defined inside a function. A variable declared inside a function without a storage class name is by default considered as an auto variable.

Syntax: auto <datatype> variable;

The features of Automatic variables are:

  • Storage : Memory
  • Initial Value : Garbage or Unpredictable
  • Scope : Within Function
  • Lifetime : Till the control remains in the function

These variables are created when the function is called and destroyed automatically when the function is exited.

Automatic variables are local to the function in which they are declared. Therefore, these values cannot be accessed by any other function. The keyword used is “auto.”

Code:

#include<stdio.h>
int main( )
{
auto int a =100; //Auto variable one
{
auto int a = 300; //Auto variable two
{
auto int a = 500; //Auto variable three
printf ("a=%d\n",a); //Prints what is within block
}
printf ("a=%d\n",a); //Prints what is within block
}
printf ("a=%d\n", a); //Prints what is within block
return 0;
}		


Output:
a=500
a=300
a=100

As Automatic variables are permitted within the block, the values of variables inside a particular block have been printed.

Extern Variables

External variables are also known as global variables. These are declared outside the function, and the values of these variables are available to all the program’s functions.

syntax : extern <datatype> variable (extern is optional as you declare it globally.)

Unlike Local Variables, Global Variables can be accessed by any function in the program. If the same name is given to both the global and local variables, priority is given to the local variable. The keyword “extern” is used to declare these variables.

The features of external variables are:

  • Storage: memory
  • Initial value: zero
  • Scope: Global
  • Lifetime: Till the program comes to an end

Code:

#include<stdio.h>
int a=20; //Global variable
int main( )
{
fun1( );
fun2( );
fun3( );
printf("\n In main function a=%d", a);
return 0;
}
void fun1( )
{
printf("\n In fun1 a = %d", a); //prints the global value
}
void fun2( )
{
int a = 10;
printf("\n In fun2 a = %d",a); //prints 10
}
void fun3( )
{
printf("\n In fun3 a = %d", a); //prints global value
}

Output:

 In fun1 a = 20
 In fun2 a = 10
 In fun3 a = 20
 In main function a=20

In this program, local variables and global variables are declared with the same name in fun2( ). In this case, when fun2( ) is called, the local variable “a” of fun2( ) overrides the global variable “a.”

Static Variables

Static variables may be Local or global depending upon where it is declared. For example, it is static global if it is declared outside the function; otherwise, it is static local if declared inside a function block.

Syntax: static <datatype> variable;

A static variable is initialized only once and can never be re-initialised. The value of the static variable persists at each call, and the last change made in the variable remains throughout the program execution. The keyword used to declare these variables is “static.”

The features of a static variable are:

  • Storage: memory
  • Initial value: zero
  • Scope: Local to the block in which the variable is defined
  • Lifetime: persists till the end of program execution

Code:

#include<stdio.h>
void incr( )
{
static int x; //default value is 1
x=x+1; 
printf("%d\n", x);
}

int main( )
{
incr( ); //prints 1
incr( ); //prints 2
incr( ); //prints 3
return 0;
}

Output:

1
2
3

As the static variables store the value, they don’t get initialised to ‘1’ for every function call. So the output increments whenever we call the function from main.

Register Variable

Instead of storing in memory, variables can also be stored in the register of the CPU. The advantage of storing in registers is that register access is faster than memory access, so frequently accessed variables are kept in registers for faster execution.

Syntax: register int count;

The keyword ‘register’ tells the compiler that the variable list is kept on the CPU registers. If the CPU fails to keep the variables in CPU registers, in that case, the variables are assured as auto and stored in the memory.

Syntax: register <datatype> variable;

Note: CPU registers are limited in number. So, we cannot declare more variables as register variables. The compiler automatically converts the register variables into non-register variables once the limit is reached.

We cannot use the register class for all types of variables. The CPU registers in the microcomputer are 16-bit registers. The data types float and double need space of more than 16 bits. If we define any variable of these types with the register class, no errors will be shown, but the compiler treats them as auto variables.

The features of register variables are:

  • Storage: Registers
  • Initial value: Garbage
  • Scope: Local
  • Lifetime: Until the control remains in that function block

Code:

#include<stdio.h>
int main( )
{
register int i; //declaration of register variable
for (i=1; i<=5; i++) 
printf ("%d\n", i); //prints 1,2,3,4,5
return 0;
}

Output:
1
2
3
4
5

The register variables work only in the particular block, so the value doesn’t change until the block expires.

Frequently Asked Questions

What do you mean by storage class?

Storage Classes are used to describe the features of a variable/function. These features include the scope, visibility, and lifetime, which help us trace a particular variable’s existence during the runtime of a program.

Is Typedef a storage class?

In C, typedef is considered a storage class like any other storage classes (auto, register, static and extern). Nevertheless, the purpose of typedef is to assign alternative names to existing types.

What are arrays in C?

An array is a collection of data items, all of the same types, accessed using a common name. For example, a one-dimensional array is like a list; A two-dimensional array is like a table; The C language places no limits on the number of dimensions in an array, though specific implementations may.

What are the functions in C?

A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.

Key Takeaways

This blog discussed the storage classes and their types, followed by exploring types of variables in storage classes and analyzing them with code.

By Dharani Mandla