C Programming Tutorial – C Storage Classes

Variables point to a memory location that has an address and a value stored. Other than this value, a variable also has a storage class. The C storage classes decides the characteristics of a variable during the execution of a program – the characteristics are storage location, initial value stored, scope, the lifetime of the variable and linkage.

To understand storage classes we must first discuss these characteristics.

Storage Location

A variable can be stored in memory or the CPU. A storage class decides the location of the value that gets stored in a variable. During its lifetime, a variable holds two things – an address to the memory location and a value of its declared data type.

Initial Value

Every variable is assigned a value by the user or the system assigns a default value to the variable because any variable that is not initialized, does not occupy a space in the memory.

Scope

The scope of the variable decides which different sections of the program can access the variable. There is many types of scope for a variable – block level scope, function level scope, function prototype level scope and source file level scope.

Lifetime of a Variable

The lifetime of the variable tells a programmer, how long the variable is going to hold its storage space and memory address during the execution of the program. After the lifetime of a variable, all resources should be accessible to other components of a program.

Linkage

The linkage tells us whether a variable is public or private. If the variable is sharable to other blocks or functions then it is public, otherwise, it is private.

There are three types of linkage – no linkage, internal linkage and external linkage. The linkage is connected to the scope of a variable. The block level scope, function level scope and the function prototype level scope does not share variables. So, there is no linkage.

The file level or program level scope can have internal or external linkage. The use of static keyword means that the file level scope has internal linkage. Variable using extern keyword means that its an external linkage.

Types of C Storage Classes

There are four storage classes

  1. Automatic
  2. Register
  3. Static
  4. External

Automatic Storage Class

The automatic storage class variables are declared using keyword – auto. If you do not declare it as auto or some other C storage classes member, by default it is auto storage class member because all variables are stored in memory.

When you create a new create a new auto storage class variable – a default garbage value is assigned automatically.

Syntax

To declare a variable as a member of automatic storage class use following syntax.

auto int number;

number = 100;

Example Program 1

[cc lang=”c” tab_size=”4″]
#include < stdio.h >

int main()
{

auto int number;

printf(“Number =%d\n”, number);

system(“PAUSE”);
return 0;

}
[/cc]

Output-1

Output1- Automatic Storage Class
Output1- Automatic Storage Class

Register Storage Class

Variables in this class are declared using the keyword – register. The variable is stored in a register, but that is not guaranteed because the storage space of CPU registers are limited. In case, registers are not available, register storage class variables treated as auto class variables and stored in memory.

The default initial value of register class is a garbage value. Each time you run the program containing register class variable a new garbage value is assigned automatically.

Since register storage class variables are stored in a register, you cannot expect to use the memory reference operator on them. It will produce errors.

The scope of the register class variable is local to its block and no linkage. Its lifetime is till the block of code is running during program execution.

Syntax

To declare a variable as register storage class type use following syntax.

register int demo_num;

Example Program 2

[cc lang=”c” tab_size=”4″]
#include

int main()
{

register int count;
short int i;
count = 0;

for(i = 0 ;i < 10; i++)
{

count+

}

printf(“count = %d\n”,count);

system(“PAUSE”);
return 0;
}
[/cc]

Output – 2

Output2 - Register Storage Class
Output2 – Register Storage Class

Static Storage Class

The static variables do not get updated when they are outside of the scope of their block. Normally you declare the static storage class variable only once during its lifetime.

They jump between function to function updating the value of the variable. Static variables are stored in a memory and by default they get a value of 0, ff a user did not initialize the static variable. The scope of the static variable is local to its block, function or function prototype or file level.

The static variable does not get destroyed immediately and they jump from function to function call, depending on how many times the function has been called.

Syntax

To declare a variable static using the keyword – static.

static int length;

Example Program 3

[cc lang=”c” tab_size=”4″]
#include

void increment();

int main()
{

increment();

/* function call */

increment();

/* function call */

increment();

/* function call */

system(“PAUSE”);
return 0;
}

void increment()
{

int length = 0;
static int width;

/* static integer automatically initialized to 0 */

printf(” Current iteration !\n\n”);

length++;
width++;

printf(“Length = %d\t Width = %d\n\n”, length, width);
}
[/cc]

Output – 3

Output3 - Static Storage Class
Output3 – Static Storage Class

External Storage Class

There is another C storage class that connects variables within a C program. The external storage class variables can be used outside of the program or file. They have an external or internal linkage. You must declare these variables using extern keyword. The external storage class variables are stored in memory.

The scope of external storage class variable is global and it can be used by many blocks, functions or files in a C program. In this way, the lifetime of the external storage class variable is as long as the program is running.

Syntax

The external variable has two cases

  1. A global variable without extern keyword.
  2. A global variable with extern keyword.

A Global Variable without extern keyword

If you declare a variable outside of main, it is external and initialized to 0 by default. These variables cannot be used outside of the current source file.

for example,

[cc lang=”c” tab_size=”4″]
int x;

int main()

{

printf(“%d\n”, x);

}
[/cc]

will print a value of 0.

A Global Variable with extern Keyword

To create an external storage class variable use the following syntax.

extern int x;

The above will only give an error because it does not do anything in memory.

Example Program 4

“file.c”
[cc lang=”c” tab_size=”4″]
#include

int x;
int main()
{

int p = x + 30;

printf(“p = %d\n”, p);

system(“PAUSE”);
return 0;

}
[/cc]
“file2.c”
[cc lang=”c” tab_size=”4″]
#include

extern int x;

x = 39;
[/cc]

Output – 4

Output 4 - External Storage Class
Output 4 – External Storage Class

Summary

A summary of all C storage classes in C programming language with each character is given below.

C Storage Class Summary
C Storage Class Summary

When to use a Storage Class ?

The usage of C storage classes depends on the programmer. If the program has constraints for which a storage class can solve the problem, then you should use a storage class variable.

Here is a list of reasons for using a storage class.

  1. When you want to use a variable that keeps the values intact between functions then use a static variable. This is a case of internal linkage.
  2. Register storage class variables are fast because they directly manipulated by a CPU, so it dies not go through the memory fetch cycles. You must use them wisely.
  3. When you have variables that are shared between files in the same program, an external storage class variable is most suitable kind of variable type for this.
  4. There are so many scenarios where a C programming storage class can be useful and solve important programming problems. It helps programmer build a useful and efficient C programs.

Bibliography

Balagurusamy, E. 2000. Programming in ANSI C. Tata McGraw-Hill Education,.

Brian W. Kernighan, Dennis M. Ritchie. 1988. C Programming Language, 2nd Edition. Prentice Hall.

 

Advertisements