Storage Classes In C language

No comments
In C language scope, visibility and lifetime are the properties of a variable or any function which are defined by the storage classes of that variable or function. Storage classes of a variable decides the storage location of a variable, life time of the variable whether it is global or a local variable and also scope of the variable ie. visibility level of a variable. Don’t worry about it if you don’t understand it completely, you will start understanding the whole concept of the storage classes as we will proceed to the types of the storage class.
Here are 4 storage classes basically used in C programming language-

    • 1.Automatic Storage Class in C

      2.Register Storage Class in C

      3.Static Storage Class in C

      4.External Storage Class in C

  • Before we start learning in detail about these 4 storage classes, let’s know about the Specifiers of these storage classes and how to declare a variable with storage classes.

    C storage Classes Specifiers-

    Storage Classes Names Storage Classes Specifiers
    Automatic Storage Class auto
    Register Storage Class register
    Static Storage Class static
    External Storage Class extern

    A storage class specifier is used usually during the declaration of the variable according to the syntax given below
    Syntax –
    specifier datat_ype variable_name;

    1.Automatic Storage Classes In C-

    Any variable which is defined within a function or a block by default belongs to a automatic storage class if no specifier is used of we use a auto specifier. All the local variables declared within a block with no specifier or a auto specifier are local to that block and they all get destroyed as soon as we exit the block. The keyword auto is rarely used for the variables declared within a block or a function body, because they are considered local or automatic storage class variables by default.
    For Example-whether we use
    int lvar;
    or
    auto int lvar;
    within a block, both are same.

    2.Register Storage Class In C-

    In C programming language register storage class is used to define local variables which are stored in register not in RAM. Register storage class is generally used for those local variables which require a quick access like counters in a program.
    Syntax-
    register int count;

    Example of Register Storage Class-
    #include <stdio.h>
    void main()
    {
    int x,y;
    register int sum;
    
    printf("\Enter first number: ");
    scanf("%d",&x);
    
    printf("\nEnter second number: ");
    scanf("%d",&y);
    
    sum = x + y;
    
    printf("\nSum of the given Numbers is : %d",sum);
    
    }
    
    In above example sum is a register class variable.

    3.Static storage class

    If we want a local variable to be available during the life-time of the program without its creation and destruction each time when it comes in and out of the scope, then we can use static storage class for that variable. Value of a static variable persists between the time when different functions calls.
    Example-
    #include <stdio.h>
    
    void add();
    void main()
    {
      add();
      add();
      add();
      add();
      }
    void add()
    {
      static int p=1;
      printf("\n%d",p);
      p=p+1;
    }  
    
    
    
    Output-
    1
    2
    3
    4
    Explanation-In above program p is a static variable, so every time when we call the function the value of the static variable persists. If the function is called again it picks up the last updated value of the static variable n and does not initializes it to 1. If we use auto storage class for the variable n then the value of the n will not be available for the next call of function add() and it will initialize to 1 again.So the output will be something like this-(try changing the storage class type to auto)
    1
    1
    1
    1

    4.External Storage Class-

    In c programming language , usually we use many functions and more then one .c files .If we want some variable to be available in all the files or functions which is declared in only one file or at one place then we use external storage class for that variable. To use a variable in some other function or file where it is not declared we use extern keyword before that variable and that variable becomes available for that file or class.
    Example- First file-one.c
    #include <stdio.h>
    
    int number ;
    extern void write_extern();
     
    main()
    {
       number = 5;
       fun_extern();
    }
    
    Second File -two.c
    #include <stdio.h>
     
    extern int count;
     
    void fun_extern(void)
    {
       printf("number is %d\n", number);
    }
    

    What If no storage class specifier is used during the declaration –

    If no storage class specifier is used during the declaration the following conditions are applied-
    • 1.A function which is declared within a function is considered extern.

      2.Any variable which is declared within a function is considered auto.

  • In this post we learned about what are storage classes in C language and their types.We also learned how to use a specific storage class and what are the outcomes of using or not using a storage class in C.I hope that you understood this storage class tutorial very well.In our next C programming tutorial we will learn about operators in C language.

    No comments :

    Post a Comment