C Storage classes, Local variable, External variable, Global variable, Register variable

Advertisements

Storage Classes in C

Storage Classes defines the scope and life time of variable declare in a function.

Types of Storage Classes

  • Auto ( Automatic ) storage class
  • Static storage class
  • Extern ( External ) storage class
  • Register storage class

Auto (Automatic) storage class - Auto variables

Auto variables have local scope. All the variables declared in function are auto, by default. We can declare same variables in different functions as well.

  • The auto keyword is optional, there is no need to write it.
  • All the formal arguments have auto scope.
  • If not initialized auto variables have garbage value.
  • The value is lost after the execution of function.

Syntax of auto storage class


              auto Data-type Variable-name;

                                  OR

              Data-type Variable-name;

Example of auto storage class


			#include<stdio.h>

              void display();
              void main()
              {
                     auto int a=10;       //OR int a=10;

                     printf("\nA : %d",a);
                     display();
                     printf("\nA : %d",a);

              }

              void display()
              {
                     int a=20;               //OR auto int a=20;

                     printf("\nA : %d",a);

              }

   Output :

              A : 10
              A : 20
              A : 10

Static storage class - Static variables

Static variable also have local scope as automatic variables. During multiple calling static variables retains their previous value.

  • We must declare variable as static.
  • Static variables can't be accessed outside the function.
  • If not initialized static variables have zero as initial value.

Syntax of static storage class


              static Data-type Variable-name;

Example of static storage class


		#include<stdio.h>

              void display();
              void main()
              {
                     display();
                     display();
                     display();
                 }

              void display()
              {
                     static int a=1;

                     printf("\nA : %d",a);

                     a++;

              }

   Output :

              A : 1
              A : 2
              A : 3

In the above example, we does not use static keyword then the output will be :


   Output :

              A : 1
              A : 1
              A : 1

Extern (External) storage class - External variables

External variables have global scope. They are declared outside all the functions.

  • The extern keyword is optional, there is no need to write it.
  • The scope of external variable is the entire program.
  • If not initialized external variable is assigned a zero value.
  • The value is not lost after the execution of function.
  • One function can assign value and another can display the value.

Syntax of external storage class or global variable


              extern Data-type Variable-name;

                                  OR

              Data-type Variable-name;

Example of external storage class or global variable


			#include<stdio.h>

              void display();
              int a=10;                     //global variable
              void main()
              {

                     printf("\nA : %d",a);
                     increment();
                     display();
                     printf("\nA : %d",a);

              }

              void increment()
              {
                     a = 20;
              }
              void display()
              {
                     printf("\nA : %d",a);
              }

   Output :

              A : 10
              A : 20
              A : 20

Register storage class - Register variables

Register variables have local scope as automatic variables. They are declared within motherboard's register memory. All the arithmetic and logical operations are carried out within these registers.

  • These variables are stored in registers. If registers are not available values are stored in main memory.
  • Register variables works faster then other kind of variables because register memory is fatser then main memory.
  • Address operator '&' can't be used with these variables.
  • Pointer to register variables are not allowed.
  • These variables are used for loops to increase efficiency.

Syntax of register storage class


              register Data-type Variable-name;

Example of register storage class


	#include<stdio.h>

              void main()
              {

                     register int a=10;

                     printf("\nA : %d",a);

              }

   Output :

              A : 10

Related topics

Advertisement