Storage Classes in C

A storage class defines the scope (visibility) and life time, visibility, memory location, and initial value of variables and/or functions within a C Program. There are four types of storage classes in C

  1. Auto
  2. Register
  3. Static
  4. External
Storage ClassesStorage PlaceDefault ValueScopeLifetime
autoRAMGarbage ValueLocalWithin function
externRAMZeroGlobalTill the end of the main program Maybe declared anywhere in the program
staticRAMZeroLocalTill the end of the main program, Retains value between multiple functions call
registerRegisterGarbage ValueLocalWithin the function

Auto

The auto storage class is the default storage class for all local variables. The keyword used to specify a variable as as auto storage class is auto. auto can only be used within functions, i.e. local variables.

  • Every local variable is automatic in C by default.
  • Automatic variables are allocated memory automatically at runtime.
  • The visibility and scope of the automatic variables is limited to the block in which they are defined.
  • The automatic variables are initialized to garbage by default.
  • The memory assigned to automatic variables gets freed upon exiting from the block.

Example 1

snippet
#include <stdio.h>
int main() {
    int a; //auto
    char b;
    float c;
    printf("%d %c %f", a, b, c); // printing initial default value of automatic variables a, b, and c. 
    return 0;
}
Output
garbage garbage garbage

Example 2

snippet
#include <stdio.h>
int main() {
    int a = 10, i;
    printf("%d ", ++a); {
        int a = 20;
        for (i = 0; i & lt; 3; i++) {
            printf("%d ", a); // 20 will be printed 3 times since it is the local value of a
        }
    }
    printf("%d ", a); // 11 will be printed since the scope of a = 20 is ended. 
}
Output
11 20 20 20 11

Register

The register storage class is used to define local variables that should be stored in a register instead of RAM(Depending upon the size of the memory remaining in the CPU). The keyword used to specify a variable as within register storage class is register. So the variable has a maximum size equal to the register size (usually one word). The register should only be used for variables that require quick access such as counters.

  • The initial default value of the register local variables is 0.
  • We can not dereference the register variables, i.e., we can not use &operator for the register variable.
  • The access time of the register variables is faster than the automatic variables.
  • We can store pointers into the register, i.e., a register can store the address of a variable.
  • Static variables can not be stored into the register since we can not use more than one storage specifier for the same variable.
Note
Defining register does not guarantee that the variable will be stored in a register. It will be stored in a register depending on hardware and implementation restrictions and also based on compilers choice.

Example 1

snippet
#include <stdio.h>
int main() {
    register int a; // variable a is allocated memory in the CPU register. The initial default value of a is 0. 
    printf("%d", a);
}
Output
0

Example 2

snippet
#include <stdio.h>
int main() {
    register int a = 0;
    printf("%u", & a); // This will give a compile time error since we can not access the address of a register variable. 
}
Output
main.c:5:5: error: address of register variable ?a? requested printf("%u",&a); ^~~~~~

Static

The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it each time it comes into and goes out of scope. The keyword used to specify a variable as within static storage class is static. The static modifier may also be applied to global variables and it causes that variable's scope to be restricted to the file in which it is declared.

  • Default initial value of the static integral variable is 0 otherwise null.
  • The variables defined as static specifier can hold their value between the multiple function calls.
  • Static local variables are visible only to the function or the block in which they are defined.
  • The visibility of the static global variable is limited to the file in which it has declared.
  • A same static variable can be declared many times but can be assigned at only one time.

Example 1

snippet
#include<stdio.h>
static char c;
static int i;
static float f;
static char s[100];
void main() {
    printf("%d %d %f %s", c, i, f); // the initial default value of c, i, and f will be printed. 
}
Output
0 0 0.000000 (null)

Example 2

snippet
#include<stdio.h>
void sum() {
    static int a = 10;
    static int b = 24;
    printf("%d %d \n", a, b);
    a++;
    b++;
}
void main() {
    int i;
    for (i = 0; i < 3; i++) {
        sum(); // The static variables holds their value between multiple function calls.  
    }
}
Output
10 24 11 25 12 26

External

The extern storage class is used to give a reference of a global variable that is visible to all the program files. The keyword used to specify a variable as within extern storage class is extern.

  • The default initial value of external integral type is 0 otherwise null.
  • The external storage class is used to tell the compiler that the variable defined as extern is declared with an external linkage elsewhere in the program.
  • The variables declared as extern are not allocated any memory. It is only declaration and intended to specify that the variable is declared elsewhere in the program.
  • We can only initialize the extern variable globally, i.e., we can not initialize the external variable within any block or method.
  • An external variable can be declared many times but can be initialized at only once.
  • If a variable is declared as external, that variable must be initialized somewhere in the program which may be extern or static. The compiler searches for it and if it is not found, then the compiler will show an error.
Note
The extern is used to declare a global variable or function in another file. The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions.

Example 1

snippet
#include <stdio.h>
int main() {
    extern int a;
    printf("%d", a);
} 
Output
main.c:(.text+0x6): undefined reference to `a' collect2: error: ld returned 1 exit status

Example 2

snippet
#include <stdio.h>
int a;
int main() {
    extern int a; // variable a is defined globally, the memory will not be allocated to a
    printf("%d", a);
}
Output
0

Example 3

snippet
#include <stdio.h>
int a;
int main() {
    extern int a = 0; // this will show a compiler error since we can not use extern and initializer at same time 
    printf("%d", a);
}
Output
compile time error main.c: In function ?main?: main.c:5:16: error: ?a? has both ?extern? and initializer extern int a = 0;

Example 4

snippet
#include <stdio.h>
int main() {
    extern int a; // Compiler will search here for a variable a defined and initialized somewhere in the pogram or not. 
    printf("%d", a);
}
int a = 20;
Output
20

Example 5

snippet
extern int a;
int a = 10;#
include < stdio.h >
    int main() {
        printf("%d", a);
    }
int a = 20; // compiler will show an error at this line
Output
compile time error
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +