Skip to main content

8. Storage Classes

 

Storage Classes

Storage classes are used to define the scope, visibility and lifetime of variables.
There are four storage classes in C, namely:

  1. Auto
  2. Extern
  3. Static
  4. Register

In addition to these four storage classes, C11 introduced a new storage class called _Thread_local, which is used to declare thread-local variables. These variables have a unique value for each thread that accesses them.

Auto

  • It is automatically allocated memory and removed from memory as per its lifetime.
  • By default all variables are auto.
    int a;
    auto int b;

Both the statements are same.

  • Default value: Garbage value.
  • Lifetime: From declared till end of block.
  • Scope: From declared point till end of block.
  1. What happens in case of a conflict between the names of a local and global variable?
    1. The global variable is given a priority.
    2. The local variable is given a priority.
    3. Which one will get a priority depends upon which one is defined first.
    4. The compiler reports an error.
  2. What is the output of the following C code?
  3. #include <stdio.h>

    int main()
    {
        int i = 10;
        {
            int i = 5;
            printf("Inside: %d\n", i);
        }
        printf("Outside: %d", i);
    }

Extern

  • Variables declared as extern have a global scope and can be accessed across multiple files.
  • They are not initialized by default and need to be defined in a separate file.
  • These are stored in data segment of the memory.
  • Default value: For int: 0
    For float: 0.000000
    For char:
  1. What is the output of the following C code?
  2. file1.c
    #include <stdio.h>

    int count;
    extern void write_extern();

    int main()
    {
        count = 5;
        write_extern();
    }

    file2.c
    #include <stdio.h>

    extern int count;

    void write_extern()
    {
        printf("count is %d", count);
    }

Static

  • Variables declared as static have a local scope and retain their value between function calls.
  • Variables declared as static are stored in the data segment of the memory.
  • Default value: For int: 0
    For float: 0.000000
    For char:
  • Lifetime: Declaration till end of program and not till end of function
  • Scope: Local variables: within block
    Global variables: within file, but cannot be accessed in multiple files
  1. What is the output of the following C code?
  2. #include <stdio.h>

    void func()
    {
        static int x = 5, y;
        printf("x = %d, y = %d\n", x, y);
        x++;
        y++;
    }

    int main()
    {
        func();
        func();
        func();
    }
  3. What is the output of the following C code?
  4. #include <stdio.h>

    int main()
    {
        static x = 10;

        static int y = 2 + 3;

        static int a = x;

        static int b = x + y;
    }

Register

  • Variables declared as register are stored in the CPU registers, which provide faster access to the variable than memory.
  • A space will be searched in registers, if space is available then space will be allocated else main memory space will be allocated.
  • Default value: Garbage value.
  • Lifetime: From declared till end of block.
  • Scope: From declared point till end of block.
  1. Which of the following statements are correct?
    1. The value stored in the CPU register can always be accessed faster than that stored in memory.
    2. A register storage variable will always be stored in a CPU register.
  2. For which of the following situation should the register storage class be used?
    1. For local variable in a function.
    2. For loop counter.
    3. For collecting values returned from a function.
    4. For variables used in recursive function.
  3. Where will the space be allocated for an automatic storage class variable?
    1. In CPU register.
    2. In memory as well as in CPU register.
    3. In memory.
    4. On disk.
Prev Next

Comments