× Home
Next Lec → ← Previous Lec

Storage classes in C. Auto, Extern, Static & Register Storage classes

Basic

Declaration→ Telling the compiler about the variable (No space reserved)
Definition→ Declaration + space reservation

What is a storage class?

  • A storage class defines scope, default intital value & lifetime of a variable.
    • A storage class defines following attributes about a variable in C.
      • Scope →where will this variable be available
      • Default initial value → If we don't initialize the variable explicitly then what will be the default value.
      • Lifetime → life of that variable available

Storage classes in C

  • In C language, following storage classes are most oftenly used:
    • Automatic variables
    • External Variables
    • Static variables
    • Register variables

Automatic variables: auto storage class

  • LOCAL VARIABLES
  • Scope→ Local to the function body they are defined in
  • Default Value → Garbage value (a random value)
  • Lifetime→ Till the end of the function block they are defined in
  • A variable defined without any storage class specification is by default an automatic variable
  • int harry and auto int harry are same

External variables: External storage class

  • They are same as global variables
  • Scope→ Global to the program they are defined in
  • Default initial value→ 0
  • Lifetime→ These variables are declared outside any function. They are available throughout the lifetime of the program.
  • A global variable can be changed by any function in the program
  • int harry written outside any function will tell compiler that harry is a global variable
  • It is recommended to minimize the use of unnecessary global bariables in a program.

External variables: Extern keyword

  • extern keyword is used to inform our C compiler that a given variable is declared somewhere else.
  • Using extern will not allocate space for the variable

Example

//saved as harry.c
int main()
{
int harry = 90;
printf("%d",harry);
}

//saved as main.c
#include "harry.c"
extern int harry;
int main()
{
harry=56;
printf("%d",harry);
}

We can also use extern as

                                
                                 #include <stdio.h>
                                
                                 int myfunc(int a, int b)
                                 {
                                    // int sum=10; //this makes a local variable and it is used
                                    extern int sum; // if we do this the compiler will know that
                                    //this is a global variable either define globally or imported from other c file
                                    return sum;
                                 }
                                 int sum = 343;
                                 int main()
                                 {
                                    int sum=myfunc(3,5);
                                    return 0;
                                 }
                                
                            

Static variables: Static storage class

  • Scope→ local to the block they are defined in
  • Defined initial value→ 0
  • lifetime→ They are available throughout the lifetime of the program
  • A static variable remains under existence for use within the function for entire program run.
  • static int harry written inside any function will tell compiler that harry is a static variable.
  • It is recommended to minimize the use of unnecessary static variables in a program
                                
                                #include <stdio.h>
                                
                                 int myfunc()
                                 {
                                    static int myVar;
                                    myVar++; //incrementing each time the function is called
                                    printf("myVar is %d\n",myVar);                                
                                 }
                                 int sum = 343;
                                 int main()
                                 {
                                    myfunc(); //calling 1st time
                                    myfunc(); //calling 2nd time
                                    myfunc(); //calling 3rd time
                                    myfunc(); //calling 4th time
                                    return 0;
                                 }
                                
                            

Output:

myVar is 1
myVar is 2
myVar is 3
myVar is 4
→ as the variable is declared as static which freeze the variable and previous value is stored in it.

Register variables: Register storage class

  • Scope→ Local to the function they are declared in
  • Default initial value→ Garbage value
  • Lifetime→ They are availbale till the end of the function block, in which the variable is defined.
  • Register variables requests the compiler to store the variable in the CPU register instead of storing in the memory to have faster access.
  • Generally this is done for the variables which are being used frequently.