Join us Now |  Forgot Password? | Forgot UserName?


Learn step by step

C - Storage Classes

A storage class defines the scope (visibility) and life-time of variables and/or a functions within a C Program. These specifiers prior the type that they modify. In C there are the following storage classes:

  • auto
  • register
  • static
  • extern
The auto Storage Class

It is the default storage class for all local variables.

{ int mount;
auto int month; }

The example above defines two variables with the same storage class, auto storage class can only be used within the functions (i.e., local variables).

The register Storage Class

The register storage class is used to define local variables that should be stored in a register instead of RAM. It also means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

{ register int miles; }

The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means it MIGHT be stored in a register depending on hardware and implementation restrictions.

The static Storage Class

The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. So, making local variables static allows them to maintain their values between function calls.

The static modifier may be applied to global variables also. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.

In C programming, when static keyword is used on a class data member, This causes only one copy of that member to be shared by all objects of its class.

#include <stdio.h>
/* function declaration */
void func(void);
static int count = 3; /* global variable */
return 0;
/* function definition */
void func( void )
static int p = 3; /* local static variable */

printf("p is %d and count is %d\n", p, count);

The extern Storage Class

The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use 'extern', the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

When you have multiple files and you define a global variable or function, That will be used in other files also, then extern keyword will be used in another file to give reference of the defined variable or function. Just for understanding it clearly, extern is used to declare a global variable or function in another file.

The extern modifier is commonly used when there are two or more files sharing the same global variables or functions as explained below.

First File: main.c
#include <stdio.h>
int count ;
extern void write_extern();
count = 5;
Second File: support.c
#include <stdio.h>
extern int count;
void write_extern(void)
printf("hello tuton count is %d\n", count);

Here, extern keyword is being used to declare count in the second file where as it has its definition in the first file, main.c. After that, compile these two files:

$gcc main.c support.c

This code will produce a.out executable program, when this is executed, it will produce the following result:


Related Videos