Storage Classes in C

Unit 1

Chapter 1: Computer Basic*
*CS101 Computer Basic is a small separate course prerequisite for KCS101/KCS201 Programming for Problem Solving – Using C

Chapter 2: Terminology

Chapter 3: Idea of Algorithm

Chapter 4: Programming Basics

Unit 2

Chapter 5: Arithmetic Expressions and Precedence

Chapter 6: Conditional Branching

Unit 3

Chapter 7: Iteration and Loops

Chapter 8: Functions

Unit 4

Chapter 9: Arrays, Structure, Union, and Enumeration

Chapter 10: Basic Algorithms and their Complexity Coexistence

Unit 5

Chapter 11: Pointers

Chapter 12: File Handling

Chapter 13: Macros & Command-Line Arguments

Appendix

KCS151/KCS251 PPS Lab Exercise

PPS Previous Year Question Papers

Storage Class

At the time of declaration each and every variable associated with a storage class. Storage class specifies some attributes of these types of objects or identifiers like where these objects are stored, their default value, scope, extent, and linkage. There are four storage class specifiers:
  1. Automatic Storage Class
  2. Register Storage Class
  3. Static Storage Class
  4. External Storage Class

Before discussing Storage Classes in deep, it is better to dig into the environment in which they are used.

Scope

The scope of an object is a region or part of the program, where the object can be accessed directly. C objects can have two types of scope:

Block (Local) Scope

Any set of statements enclosed in curly braces { } is called a block. For example, the body of a function is a block and a compound statement of the loop is a nested block within its function block. objects with block scope can be accessed only within the block in which they are declared from its declaration point to the end of the block. Objects with block scope are sometimes referred to as local objects.

File (Global) Scope

A large program can have multiple source files. The file scope covers the entire source file with the files included in it. An object with file scope can be accessed within the file in which it is declared from its declaration point to the end of the file. Objects with file scope are sometimes referred to as global objects.

Extent

The extent is also known as storage duration. The extent of an object is the duration, for which the memory is allocated to the object. C objects can have four types of extent:

Automatic Extent

It is the most common and simplest form of extent. An object with the automatic extent gets the memory allocated each time its declaration is encountered and free the memory each time its block is exited. For example, a variable declared within the body of a loop get and free the memory in each iteration.

Static Extent

An object with static extent gets the memory allocated when the program is loaded for execution and free the memory at the end of the program. It remains intact no matter how many times its declaration is encountered. For example, if a variable with static extent declared within the body of a function. It gets its memory allocated at the time the program is loaded for execution. It does not free the memory until the end of the program no matter how many times the function is called and complete its execution.

Dynamic Extent

Dynamic memory allocation is used to create a single large block of memory at run time. It can be achieved by using C library functions such as malloc, calloc, realloc, etc. Memory created by these functions has a dynamic extent. The objects can acquire such types of memory and free it at run time as per the requirement.

Linkage

Linkage is different from the scope, however, sometimes it is difficult to distinguish between scope and linkage as they look quite similar. A large C program may consist of more than one source file which is potentially written by a different programmer. These source files with their own objects are compiled one at a time. Until the compilation process, an object can be identified by its scope. After compilation, Linker links all the compiled machine code files together to build an executable file. Linkage describes whether an object can be accessed only within the file in which it is declared or it can be accessed from other linked files. Thus, the linkage is a property handled by the linker, whereas the scope is a property handled by the compiler. There are two types of linkage:

Internal Linkage

An object with internal linkage can be accessed only within the file it is declared. It cannot be referred from other linked files.

External Linkage

An object with external linkage declared in one file can also be accessed from other linked files of the program as well as within the file it is declared.

Memory Location

At the time of program execution, memory is assigned to these objects. They can be stored in the program stack, data segment, heap or CPU registers. The storage class defines the memory location to store these objects.

Default Value

The storage class decides the initial value of the objects. However, an object can be initialized with a value at the time of its declaration but if no value is assigned to the object, it stored its initial value decided by the storage class. It may be zero or a garbage value.

Automatic Storage Class

Objects of automatic storage class are declared at the beginning of a block with block scope. These objects can be accessed only within the block in which they are declared. The extent of these objects is automatic so the memory is allocated each time their declaration is encountered and set free each time the block is exited. The linkage of these objects is internal so these objects could not be accessed from other linked files. The memory location of these objects is the program’s stack in RAM (Random Access Memory) and the default value of these objects is garbage if not initialized. Keyword auto is used to denote automatic storage class, however, it is optional. Every variable declared at the beginning of a block with no specifier is considered as an auto variable.
Example:
				
					/*
 File: Auto.c
 Author: Aditya Saini
 Date: July 24, 2019
 Description: Program to demonstrate the use of Automatic Storage Class.
*/

#include <stdio.h>

int main (void)
{
    int i;
    for ( i = 1; i < = 4; i++ )
    {
        int x = 10;
        printf ( "Value of x in iteration %d is :   %d\n", i, x );
        x = x + 10;
    }
    return 0;
};
				
			

Try it yourself

In the above example, both i and x are auto variables.

Register Storage Class

The register storage class is the same as the automatic storage class. The only difference between them is the storage location. The register storage class specifier includes a recommendation to the compiler that the object should be stored in CPU registers. CPU registers are faster than RAM. Objects stored in CPU registers can be accessed faster than the objects stored in RAM. To improve the efficiency of the program, frequently used objects are stored in CPU registers. Because of the limited size and numbers of CPU registers, only a few objects can be stored in CPU registers so it is not sure that an object declared with the register storage class specifier will store in the CPU register. If the compiler does not allocate CPU register to an object, it is treated as an automatic object. The keyword register is used to denote register storage class.
Note: The register storage class specifier cannot be used with file scope (global) objects. Also, the CPU register does not have an address so the address operator (&) could not be applied to an object with register storage class specifier.
Example:
				
					/*
 File: Register.c
 Author: Aditya Saini
 Date: July 24, 2019
 Description: Program to demonstrate the use of Automatic Storage Class.
*/

#include <stdio.h>

int main (void)
{
    register int i;
    for ( i = 1; i <= 4; i++ )
    {
        int x = 10;
        printf ( "Value of x in iteration %d is :   %d\n", i, x );
        x = x + 10;
    }
    return 0;
};
				
			

Try it yourself

In the above example, i is a register variable and x is an auto variable.

Static Storage Class

Objects of static storage class can either be declared at the beginning of a block with block scope or can be declared as a global object with file scope. Objects with block scope can be accessed only within the block in which they are declared whereas, objects with file scope can be accessed within the file in which they are declared. The extent of both types of objects is static so the memory is allocated when the program is loaded for execution and free the memory at the end of the program. It remains intact no matter how many times its declaration is encountered.
The linkage of both types of objects is internal so these objects could not be accessed from other linked files. The memory location of these objects is the data segment and the default value of these objects is zero if not initialized. Keyword static is used to denote a static storage class.

Example Static Storage Class with Block Scope:

				
					/*
 File: b_static.c
 Author: Aditya Saini
 Date: July 24, 2019
 Description: Program to demonstrate the use of Static Storage Class with Block Scope.
*/

#include <stdio.h>

int main (void)
{
    int i;
    for ( i = 1; i <= 4; i++ )
    {
        static int x = 10;
        printf ( "Value of x in iteration %d is :   %d\n", i, x );
        x = x + 10;
    }
    return 0;
};
				
			

Try it yourself

In the above example, i is an auto variable and x is a static variable with block scope.

Example Static Storage Class with File Scope:

				
					/*
 File: f_static.c
 Author: Aditya Saini
 Date: July 24, 2019
 Description: Program to demonstrate the use of Static Storage Class with File Scope.
*/

#include <stdio.h>

void fun (void);

static int x = 7;

int main (void)
{
    printf ( "Value of x in main is :   %d\n", x );
    fun ( );
    printf ( "Value of x in main is :   %d\n", x );
    return 0;
};

void fun (void)
{
    x++;
    printf ( "Value of x in fun is :   %d\n", x );
    return;
};
				
			

Try it yourself

In the above example, x is a static variable with file scope.

External Storage Class

It is common, on large projects, to decompose the project into many source files. These files are compiled separately and linked together to form the complete software as a single unit.

Objects of extern storage class defined in one source file can also be accessed from all the source files in which they are declared. These objects are generally declared at the global level with the keyword extern. These objects have file scope with static extent but the linkage is external. The memory location of these objects is the data segment and the default value of these objects is zero if not initialized.

This design leads to a confusion that which file defines the object and which are simply declaring the variable? Actually, it depends on whether the object is initialized or not.

  1. If the object is not initialized, then the first declaration seen by the linkage editor or linker is considered as a definition and all others as a declaration to reference it.
  2. If the object is initialized somewhere, then it is considered to be as defining declaration.
The keyword extern must be explicitly used in source files that use the reference of an external object. It can be omitted in the source file that defines or initialize the object.
Example:
First File: f_extern.c
				
					/*
 File: f_extern.c
 Author: Aditya Saini
 Date: July 24, 2019
 Description: Program to demonstrate the use of Extern Storage Class.
*/

#include <stdio.h>

extern int extern_var;

int main (void)
{
    printf ("External Variable: %d", extern_var);
    return 0;
};
				
			
Second File: s_extern.c
				
					/*
 File: s_extern.c
 Author: Aditya Saini
 Date: July 24, 2019
 Description: Program to demonstrate the use of Extern Storage Class.
*/

#include <stdio.h>

extern_var = 10;
				
			
In the above example, extern_var is an extern variable.

Note: In order to compile and run the above code, you need a local IDE.

Summary

Storage ClassKeywordScopeExtentLinkageMemory LocationLifetimeDefault Value
Automaticauto or noneBlockAutomaticInternalProgram StackTill end of BlockGarbage
RegisterregisterBlockAutomaticInternalPreferably CPU Register or Program StackTill end of BlockGarbage
StaticstaticBlock or FileStaticInternalData SegmentTill end of the ProgramZero
Externextern or noneFileStaticExternalData SegmentTill end of the ProgramZero

Aditya Saini

Please Share:
Facebook
Twitter
WhatsApp
Telegram
LinkedIn
Pinterest
Reddit
Tumblr
Email
Print

Leave a Comment

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

Scroll to Top
Scroll to Top