Storage classes in C


Advertisements

Share



What are storage classes?

Storage class determines the scope, lifetime, storage location and initial value of a variable. There are four storage classes in C such as Automatic, Static, Register, External

What are storage class specifiers?

A variable of each storage class can be declared by writing a storage specifier in front of the variable. At most one storage specifier can be written in front of a variable. The storage specifiers for each storage class is as follows:

Automatic storage class

Variables with automatic storage class are defined inside a function and are also called local variables. Auto/local variables have the following rules.

A variable declared outside all the functions is called a global variable. Global variables have the following rules.

To demonstrate the auto/local, global variable features, let's write a program as shown below.

Program to illustrate a auto/local, global variables

#include <stdio.h>
int autoFunc(int );
int glbvar = 10;
int glbvar1;
int main() {
  printf("glbvar:%d,glbvar1:%d\n", glbvar, glbvar1);
  int autovar = 20;
  int res;
  printf("res is:%d\n", res);
  res = autoFunc(autovar);
  printf("res is:%d\n", res);
  return 0;
}
int autoFunc(int autovar)
{
  printf("0th block:%d\n", autovar);
  {
    auto int autovar = 30;
    printf("Ist block:%d\n", autovar);
    {
      auto int autovar = 40;
      printf("2nd block:%d\n", autovar);
    }
  }
  glbvar += autovar;
  return glbvar;
}
    Above program will produce the following output.
$ gcc Listing1.c
$ ./a.out
glbvar:10,glbvar1:0
res is:0
0th block:20
Ist block:30
2nd block:40
res is:30
$

Let's analyze the above program. The following are the points to observe from the above program.

Static storage class

A static variable remains in memory while the program is running in contrast to a normal or auto variable which is destroyed when a function call where the variable was declared is over. Following are the features of a static variable:

Let's write a program to demonstrate the use of static variables


#include <stdio.h>
static int p;
int f(void) {
  static int x = 0;
  x++;
  return x;
}
int y(void) {
  int x = 0;
  x++;
  return x;
}
int main() 
{
  int j;
  printf("Static variable x:%d\n", p);
  for (j = 0; j < 5; j++) {
    printf("Value of f(): %d\n", f());
  }
  for (j = 0; j < 5; j++) {
    printf("Value of y(): %d\n", y());
  }
  return 0;
}
   Above program will produce the following output.
$ gcc prog1.c
$ ./a.out
Static variable x:0
Value of f(): 1
Value of f(): 2
Value of f(): 3
Value of f(): 4
Value of f(): 5
Value of y(): 1
Value of y(): 1
Value of y(): 1
Value of y(): 1
Value of y(): 1
pi@raspberrypi:~/try $

Let's analyze the above program. Here we have two functions f and y. Function f is having a static int variable x where as function y is having a non-static variable x. You can see in the output that though we called both the functions 5 times, output of function f keeps incrementing on successive calls where as the output of function y remains always at 1. This is because the static variable x declared inside function f is not reinitilized to 0 where as in function y variable x is reinitialized to 0 on successive calls.

Also when we printed an uninitialized static variable p, inside main function, compiler automatically initilized to 0. So when we printed it inside main function, it prints as 0.

Let's write another program and compile along with the above program as shown below.


#include <stdio.h>
extern int p;
int function()
{
  printf("printing static var p:%d\n", p);
}
   When prog1.c and prog2.c are compiled together, the complier will give the following error.
pi@raspberrypi:~/try $ gcc prog1.c prog2.c
/tmp/cciYesJG.o: In function `function':
prog2.c:(.text+0x28): undefined reference to `p'
collect2: error: ld returned 1 exit status
pi@raspberrypi:~/try $

If you see the above program output, it shows that the variable p which is declared as static in prog1.c could not be accessed in the prog2.c even though extern keyword is used to access it from prog1.c file. So it confirms that static variable can be accessed only in the file where it is declared.

Now remobve the static storage specifier from variable p in prog1.c. You should be able to compile prog1.c and prog2.c together. Compiler will not give any error

Register storage class

Register variables indicate the compiler to store the variable in CPU register instead of memory. The register storage class specifier is typically specified for frequently used variables, such as a loop control variable, to enhance performance by reducing access time. But compiler is not required to commit this request. Because of the limited size and number of registers available on most systems, only few variables can actually be put in CPU registers. If the compiler does not allocate a CPU register for a register variable, the variable is treated as having the storage class specifier auto.

    The following are the rules of use for register variables:
    The following program shows the errors, declarations for register variables.
Program to demonstrate wrong use cases of register variable

#include <stdio.h>
register int r; /* Wrong declaration */
int main() {
  register int var; 
  int *b = &var; /* Not allowed */
  return 0;
}

Extern storage class

An external variable is a variable declared as global in another file and is extracted from that file for using in the current file.

    The following are the rules of use for extern variables:
    Let's write a small program to demonstrate the use of external storage class.
Program to demonstrate use of exern storage class

/* File ext1.c */
#include <stdio.h>
void call_fun();
int val2 = 90;
int main()
{
  call_fun();
  return 0;
}

/* File ext2.c */
#include <stdio.h>
extern int val2;
void call_fun()
{
  printf("The value of val2 is %d\n",val2);
}
   Above program will produce the following output.
$ gcc ext1.c ext2.c
$ ./a.out
The value of val2 is 90
$

Exercises, Solutions

High
Medium
Low
High
Low
Medium
High
Medium
Medium
Medium

Want to contribute a new article? Write and upload your article information .
Share

Articles


C Programming

More Articles Categories

Technical articles

Prepare for software jobs


Test your skills