Storage class in C decides the part of storage to allocate memory for a variable, it also determines the scope of a variable. All variables defined in a C program get some physical location in memory where variable's value is stored. Memory and CPU registers are types of memory locations where a variable's value can be stored. The storage class of a variable in C determines the life time of the variable if this is 'global' or 'local'. Along with the life time of a variable, storage class also determines variable's storage location (memory or registers), the scope (visibility level) of the variable, and the initial value of the variable. There are four storage classes in C those are automatic, register, static, and external.
There are four storage class specifiers in C as follows, typedef
specifier does not reserve storage and is called a storage class specifier only for syntactic convenience. It is not a storage class specifier in the common meaning.
These specifiers tell the compiler how to store the subsequent variable. The general form of a variable declaration that uses a storage class is shown here:
storage_class_specifier data_type variable_name;
At most one storage class specifier may be given in a declaration. If no storage class specifier is specified then following rules are used:
auto
.extern
.static
, with external linkage.
Variables and functions having external linkage are available to all files that constitute a program. File scope variables and functions declared as static
(described shortly) have internal linkage. These are known only within the file in which they are declared. Local variables have no linkage and are therefore known only within their own block.
There are four storage classes in C they are as follows:
Now, let us discuss these storage classes one by one.
A variable defined within a function or block with auto
specifier belongs to automatic storage class. All variables defined within a function or block by default belong to automatic storage class if no storage class is mentioned. Variables having automatic storage class are local to the block which they are defined in, and get destroyed on exit from the block.
The following C program demonstrates the visibility level of auto
variables.
#include <stdio.h> int main( ) { auto int i = 1; { auto int i = 2; { auto int i = 3; printf ( "\n%d ", i); } printf ( "%d ", i); } printf( "%d\n", i); } OUTPUT ====== 3 2 1
In above example program you see three definitions for variable i
. Here, you may be thinking if there could be more than one variable with the same name. Yes, there could be if these variables are defined in different blocks. So, there will be no error here and the program will compile and execute successfully. The printf
in the inner most block will print 3 and the variable i
defined in the inner most block gets destroyed as soon as control exits from the block. Now control comes to the second outer block and prints 2 then comes to the outer block and prints 1. Here, note that automatic variables must always be initialized properly, otherwise you are likely to get unexpected results because automatic variables are not given any initial value by the compiler.
The register
specifier declares a variable of register storage class. Variables belonging to register storage class are local to the block which they are defined in, and get destroyed on exit from the block. A register
declaration is equivalent to an auto
declaration, but hints that the declared variable will be accessed frequently; therefore they are placed in CPU registers, not in memory. Only a few variables are actually placed into registers, and only certain types are eligible; the restrictions are implementation-dependent. However, if a variable is declared register, the unary &
(address of) operator may not be applied to it, explicitly or implicitly. Register variables are also given no initial value by the compiler.
The following piece of code is trying to get the address of variable i
into pointer variable p
but it won't succeed because i
is declared register
; therefore following piece of code won't compile and exit with error "error: address of register variable requested".
#include <stdio.h> int main() { register int i = 10; int *p = &i; //error: address of register variable requested printf("Value of i: %d", *p); printf("Address of i: %u", p); }
The static
specifier gives the declared variable static storage class. Static variables can be used within function or file.Unlike global variables, static variables are not visible outside their function or file, but they maintain their values between calls. The static
specifier has different effects upon local and global variables. See the following flavours of static
specifier.
static
specifier is applied to a local variable inside a function or block, the compiler creates permanent storage for it, much as it creates storage for a global variable but static local variable remains visible only to the function or block in which it is defined. In simple terms, a static local variable is a local variable that retains its value between function calls. For example, the following program code defines static
variable i
at two places in two blocks inside function staticDemo()
. Function staticDemo()
is called twice within from main
function. During second call static variables retain their old values and they are not initialized again in second call of staticDemo()
.
#include <stdio.h> void staticDemo() { static int i; { static int i = 1; printf("%d ", i); i++; } printf("%d\n", i); i++; } int main() { staticDemo(); staticDemo(); } OUTPUT ====== 1 0 2 1
static
specifier is applied to a global variable or a function then compiler makes that variable or function known only to the file in which it is defined. A static global variable has internal linkage that means even though the variable is global; routines in other files have no knowledge of it and cannot access and alter its contents directly. The following C program defines one static global variable gInt
and a static function staticDemo()
, for the variable and function are defined static they cannot be used outside the file (translation unit) staticdemo.c.
.
Static variables have default initial value zero and initialized only once in their lifetime.
The extern
specifier gives the declared variable external storage class. The principal use of extern
is to specify that a variable is declared with external linkage elsewhere in the program. To understand why this is important, it is necessary to understand the difference between a declaration and a definition. A declaration declares the name and type of a variable or function. A definition causes storage to be allocated for the variable or the body of the function to be defined. The same variable or function may have many declarations, but there can be only one definition for that variable or function.
When extern
specifier is used with a variable declaration then no storage is allocated to that variable and it is assumed that the variable has already been defined elsewhere in the program. When we use extern
specifier the variable cannot be initialized because with extern
specifier variable is declared, not defined.
In the following sample C program if you remove extern int x;
you will get an error "Undeclared identifier 'x'" because variable x
is defined later than it has been used in printf
. In this example, the extern
specifier tells the compiler that variable x
has already been defined and it is declared here for compiler's information.
#include <stdio.h> extern int x; int main() { printf("x: %d\n", x); } int x = 10;
Also, if you change the statement extern int x;
to extern int x = 50;
you will again get an error "Redefinition of 'x'" because with extern
specifier the variable cannot be initialized, if it is defined elsewhere. If not then extern
declaration becomes a definition.
Note that extern
can also be applied to a function declaration, but doing so is redundant because all function declarations are implicitly extern
.
In this tutorial we discussed storage classes and storage class specifiers in C. Hope you have enjoyed reading this article. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
register
specifier got stored in CPU register?extern
variable which has already been defined?
Share this page on WhatsApp