Q2 Write a C program to show the storage, default initial value, scope and life of automatic storage class variables.

Write a C program to show the storage, default initial value, scope and life of automatic storage class variables.

Program: 140

Write a C program to show the storage, default initial value, scope and life of automatic storage class variables.

Variable Storage Class tells us:

  1. Storage (location)
  2. Default Initial Value of the variable
  3. Scope of the variable
  4. Life of the variable

1. Storage: Where the variable would be stored.

2. Default Initial Value: What will be the initial value of the variable, if initial value is not specifically assigned. (i.e. the default initial value)

3. Scope: What is the scope of the variable: i.e. in which functions the value of the variable would be available.

4. Life: What is the life of the variable: i.e. how long would the variable exist.

Automatic Storage Class:

Storage : Memory
Default Initial Values : A garbage Value (unpredictable value)
Scope : Local to the block remains within the block in which the variable defined.
Life : Till the control remains within the block in which the variable is defined.

#include<stdio.h>
int main()
{

    auto int i, j;
    auto int a=1;

    //Print garbage value (or unpredictable value)
    printf("%d %d\n", i,j);

    //Scope of variables
    //auto int a=1;
    {
        auto int a=2;
        {
            auto int a=3;
            printf("%d ",a);
        }
        printf("%d ",a);
    }
    printf("%d \n",a);

    return 0;
}

 

Output:

16 0
3 2 1

Lokesh Kumar: Being EASTER SCIENCE's founder, Lokesh Kumar wants to share his knowledge and ideas. His motive is "We assist you to choose the best", He believes in different thinking.
Related Post
Leave a Comment

This website uses cookies.