C tutorial: Storage Class in C programming language

Programming languages or concepts

Storage Classes in C programming language:-

 What is Storage Classes:-

   A storage classes represents the visibility and location of a variable.

Describe the following things

Initialised value of variable.

Variable is not only associated with data type.

Its value but also a storage classes

storage class defines the visibility and lifetime of variables and functions within C program.


it has four types of storage classes in C:
1. Auto
    2. Extern
      3. Register
 4. Static


1). Auto:-

 Auto is the default storage class for every local variables. 

 when a function is called they are created and function is exist they are destroyed automatically.

The scope of an auto variable is limited with the specific block only.

Auto Storage Class is define using Auto Keyword.

 It is not explicity specified.

Ex.  auto int age;


Small Program

int add(void) 

{

     int x=99;

     auto int x ;

     

}

The above example defines two variables with in the same storage class. 'auto' can only be used within functions, i.e., as a local variables.


2). Extern:-

        Extern stands for external storage class.

The extern storage class is used to give a reference of a global variable, and it is visible to ALL the program files.

  the variables defined using an extern keyword are called as  global variable.

   These values are accessible throughout the program.

  the variable cannot be initialized however it has already been define in the original file.

   Ex.

             extern void display ( );

   The extern variables declaration does not allocate storage for variables.

 

Program file 1

#include<studio.h>

   int a;

    Void main ( )

     {

         extern int a;

         printf ("%d",a);

 }



Program file 2


#include<studio.h>

    int a = 99;


3). Register:-

 It define the local variable which are stored in a register rather than RAM.

 Registers variables have faster access as compared to the normal variable.

The register storage classes is used to define local variables it should be stored in a register instead of RAM. This means that the variables has a maximum size equal to the register size

   The means that the variable has a maximum size equal to the register size and cannot have the unary ' & ' operator applied to it.

   deregister should only be used for variables that required quick access such as a counters.

   The  keyword register is used to declare a register storage classes or register VAriables.

   the variable declared using register storage class is has lifespan throughout the program.

  It is similar to the auto storage classes.

Ex.  register int data;

  

#include<studio.h>

main ( )

{

    register int height;

    int *ptr = & height;

}



4). Static:-

 The static storage class instructs the compiler to stay 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.

  Static local variable is a local variable that retains and stores its value between function calls or block and remains visible only to the function or block in which it is defined.

Static modifier may also be applied to global variable.  Waste it is used on a global variable it access only one coffee of that member to be shared by all the object of its class. 

Ex.

static int count = 10;

close