Different Types of Storage Classes in C with Examples

What are Storage Classes in C?

The storage space allocates storage classes in C for a variable that will be kept in memory. They are kept in a system’s RAM.

They decide on a variable’s range in addition to its storage capacity.

The random memory primarily consists of the device’s memory and CPU registers, where variables in the C program are physically stored.

The lifespan of the variable is also specified by storage classes in C, which designates it as “local” or “global”.

Storage classes can also specify a variable’s scope, visibility, and starting value. The four main storage classes in C are automatic, register, static, and external.

Each will be further discussed one at a time.

Storage classesPurpose
autoIt is a storage class by default.
externA global variable
staticIt is a local variable that can still produce a value even after control is passed to the calling function.
registerIt’s a variable that’s kept in a register.

How are Storage Classes in C declared?

The storage class specifiers auto, register, extern, and static are used to declare the four storage classes in C in a block or program.

Another storage class specifier that does not reserve storage is “typedef”, which is used in the syntactic form.

The specifiers give the compiler instructions on how to store the variables. The C compiler is informed that a variable has been declared with external linkage by the external storage class.

The distinction between defining and declaring a variable is significant. A variable is defined when memory is set aside for it, and it is declared when a value is first initialized in it.

Special Case: When software doesn’t declare or define a storage class specifier

The variable declaration includes at least one storage class specifier.

However, in the absence of a storage class specifier, the following guidelines are followed:

  • Auto variables are those that have been declared inside of a function.

  • Functions that are declared inside another function are regarded as externs.

  • Static variables and functions with external linkage are those that have been defined outside of a function.

What are the Types of Storage Classes in C?

Let’s take a closer look at C’s four storage classes:

Automatic Storage Class in C with Example

Automatic Storage Classes in C – If no storage class is specified, every variable defined in a function or block is automatically stored in the automatic storage class.

The auto specifier is used to declare variables that are members of the automatic storage class in a function or block.

In C, variables declared as auto are exclusive to the block in which they are defined and are discarded outside of it.

Example Program That Displays Automatic Storage Classes in C

#include <stdio.h>
int main( )
{
auto int a = 1;
{
auto int a = 2;
{
auto int a = 3;
printf ( "%d" , a);
}
printf ( "%d" , a);
}
printf( "%d" , a);
}

The program’s output is:

321

Explanation:

The variable a is declared three times in the program mentioned above. The same name variables may be declared in many blocks.

As a result, this program will properly compile and run without any errors. The innermost block’s function “printf” will output “3”, and the block’s final statement will cause the variable I to disappear.

The subsequent outside block, the second, prints 2, and is followed by the subsequent outer block, which prints 1.

You must appropriately initialize the automatic variables. In order to avoid getting undefined values since the compiler does not set an initial value for them.

You can test the above example here! ➡ C Online Compiler

Register Storage Class in C with Example

Register Storage Classes in C – The variables that make up a register storage class are comparable to auto in C but are, as the name implies, stored in CPU registers rather than memory.

They are the ones that are commonly accessed. The register storage class variable is declared using the register specifier.

A register storage class’s variables are confined to the block in which they are defined and are destroyed at the block’s conclusion.

Example Program That Displays Register Storage Classes in C

#include <stdio.h>
int main()
{
register int a = 10;
int *q = &a; //error: address of register variable requested
printf("Value of a: %d", *q);
printf("Address of a: %u", q);
}

The program’s output is:

jdoodle.c: In function ‘main’:
jdoodle.c:9:1: error: address of register variable ‘a’ requested
9 | int *q = &a; //error: address of register variable requested

Explanation:

The error message “Error: address of register variable requested” will be displayed in the program above when the code tries to get the address of variable I into the pointer variable p.

However, because a is declared as a register variable, the code won’t compile.

Registers can only contain specific kinds of variables. The compiler does not set an initial value for register variables.

You can test the above example here! ➡ C Online Compiler

Static Storage Class in C with Example

Static Storage Classes in C – Static variables have no visibility outside of the function or file in which they are defined, but their values are preserved between calls.

The static specifier is used to declare variables with a static storage class. Within a function or file are static variables. With local and global variables, the static specifier behaves differently.

Example Program That Displays Static Storage Classes in C with Local Variables

#include <stdio.h>
void staticDemo()
{
 static int a;
{
 static int a = 1;
  printf("%d ", a);
a++;
}
  printf("%d ", a);
  a++;
}
int main()
{
  staticDemo();
  staticDemo();
}

The output of the Program:

1 0
2 1

Explanation:

Permanent storage space is produced in the compiler when a local variable is defined by a static specifier inside a function or block.

The static local variable keeps its value in between function calls and is accessible to the function or block where it is declared.

The static variable a is defined twice in the program above, inside the staticDemo() function, in two different blocks. In the main function, staticDemo() is called twice.

The static variables keep their previous values and do not need to be initialized anew during the subsequent call.

You can test the above example here! ➡ C Online Compiler

Example Program That Displays Static Storage Classes in C with Global Variables

#include <stdio.h>
static int globalInt = 1;
static void staticDemo()
{
  static int a;
  printf("%d ", a);
  a++;
  printf("%d ", globalInt);
  globalInt++;
}
int main()
{
  staticDemo();
  staticDemo();
}

The output of the Program:

0 1
1 2

Explanation:

Static variables are preserved for the duration of the program and only require a single initialization.

They start out with a zero initial value by default.

If a static specifier is used to declare a global variable or function, only the file in which it is defined will have access to that variable or function.

Since a static global variable has internal linkage, other file functions cannot read and modify its contents.

The global static variable globalInt and the static function staticDemo() are defined as static in the aforementioned program and cannot be utilized outside of the C file.\

You can test the above example here! ➡ C Online Compiler

External Storage Class in C with Examples

External Storage Classes in C – The ‘extern‘ specifier declares variables or functions of the external storage class.

A variable is assumed to have been defined elsewhere in the program when it is declared with the extern specifier.

Which prevents any storage from being assigned to it.

The variable is not initialized when using an extern specifier. It is necessary to declare a variable with external linkage when using an extern to specify it in a program.

Example Program That Displays External Storage Classes in C

#include <stdio.h>
extern int a;
int main()
{
printf("a: %d", a);
}
int a = 1;

The output of the Program:

a: 1

Explanation:

The error “Undeclared identifier a will appear in the C program above if extern int a is removed since the variable a is defined after being used in print.

The compiler is informed by the extern specifier that the variable a has been defined and is being declared here.

The error “Redefinition of a will appear if you alter extern int a to extern int a= 5; because the extern specifier does not initialize a variable.

You can test the above example here! ➡ C Online Compiler

The following table lists the key characteristics of each storage type that is frequently used in C programming.

Storage ClassDeclarationStorageDefault Initial ValueScopeLifetime
autoInside a function/blockMemoryUnpredictableWithin the function/blockWithin the function/block
registerInside a function/blockCPU RegistersGarbageWithin the function/blockWithin the function/block
externOutside all functionsMemoryZeroEntire the file and other files where the variable is declared as externprogram runtime
Static (local)Inside a function/blockMemoryZeroWithin the function/blockprogram runtime
Static (global)Outside all functionsMemoryZeroGlobalprogram runtime

Conclusion

This article explains the idea of storage classes in C and describes how each type is unique. The assignment as well as the scope and lifetime.

The visibility of the variable you’re working with determines when to employ a specific storage type.

In C, extra details about a variable are represented by a storage class.

In the C programming language, variables, functions, and parameters are defined using a storage class specifier.

Rather than storing the variable in memory for quick access, a local variable created within a block or function register is utilized instead.

Both global and local variables can be described as static. Within a C program, each has a specific use case.

Data sharing between C project files is accomplished via Extern.

I hope you enjoy this post on Constant in C, in which I attempt to illustrate the language’s core grammar.


Leave a Comment