Basics of Storage Classes in C - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Basics of Storage Classes in C

Share This

In C programming language, each variable is associated with a storage class, which represents the memory location, lifetime, scope, and visibility of the variable. In this tutorial, we will discuss the storage class with a suitable example. 


Storage Class: auto

An auto variable is a local variable. If we do not mention the scope of the variable at the time of variable declaration, the default scope of the variable is auto. Thus, the use of the keyword 'auto' at the time of declaration is redundant. Still, if we want to use the keyword, the variable is declared as follows:

auto int x = 10;

Note that the scope of x is local to the block in which it is declared. If a variable is declared as a local variable in a function, its scope is within the function only.
auto variables are stored within the Stack segment of the memory.


Storage Class: static

A static variable is a special type of variable, which holds and preserves its value for the future. For static variables, the memory is allocated only once. Hence, its previous values can be accessed in the future.

Even if a static variable is declared within a function or block with local scope, the value of a static variable is preserved for the future. Hence, if the value of the variable is changed in a function call, the updated value will be received in the next call.

To declare a variable as static, the variable is declared as follows:

static int x = 10;
static variables are stored within the Data segment of the memory.

Storage Class: extern

If we are working with multiple files, the variables and functions declared and defined in a file need to be accessed from other files. The keyword extern is used to declare external variables and functions. Thus, it indicates that the variable is declared in a different file. An external variable is declared as below:

extern int x = 10;
extern variables are also stored within Data segment of the memory.

Let's check an example in our tutorial Multi-file Programming in C and Code Reuse

Storage Class: register

A register variable is preferably stored in the register if a register is available. Otherwise, it works as an auto variable. The variable, which is used very frequently, is preferred to be declared as a register variable as their access time is faster. A register variable is declared as below:

register int x = 10;

If many variables are declared as register, we will not achieve any performance improvement. Moreover, note that the address of a register variable can not be accessed. Thus, it can not be accessed by the pointer variable.


Difference between auto, register, and static variable

If you run the following code, you will see that the value of variables x and y are initialized in every function call. Whereas, the value of z is being changed. The reason is once a static variable is declared and initialized by some value, later on, if you access the same variable, the previously stored data is being accessed.
As the register variables are preferably stored in CPU registers, their addresses can not be accessed.

#include <stdio.h> void fun(); int main() { fun(); fun(); return 0; } void fun() { auto int x = 1; register int y = 1; static int z = 1; printf("address of x is %u, address of z is %u\n", &x, &z); //printf("address of y is %u\n", &y); -- can not be requested printf("x is %d, y is %d, z is %d\n", x++, y++, z++); }

Happy Exploring!

No comments:

Post a Comment