storage classes in c
C Programming

Storage Classes in C

Storage Classes in C

  • Storage Classes in C are used to describe the scope ad lifetime of a variable and function in C Programming.
  • Questions based on Storage classes are generally asked in GATE(CS)  and UGC NET Exam.

Dear Students Today we will learn about the concepts of storage class in C and the related concepts such as scope, default initial value and lifetime of a variable with suitable example and program in this tutorial.

Frequently Asked Questions

After reading this tutorial students will be able to answer the following questions asked in technical interview.

  • What is Storage Class Specifier in C ?
  • Which is the fastest Storage Class in C ?
  • What is the significance of storage class in C ?
  • What is Auto in C ?
  • What is meaning of extern in C ?
  • Can we use extern static variable in C ?

Let’s start with introduction of storage class in C for better understanding of the related concepts.

What is Storage Class in C ?

  • Memory management is a very important feature of c-programming.
  • Use of different storage class provides a way of better memory management in C.
  • A storage class defines scope, default initial value and lifetime of a variable.
  • The meaning of Scope, default initial value and lifetime of a variable is explained here.

What is Scope of a Variable ?

  • Scope of a variable is the availability of the variable in program.
  • It says whether the variable can be used within the function or can be used within the main function or can be used anywhere in the program.

Default initial value of Variable in C

  • If we don’t initialize the variable explicitly, the default value taken by the variable is known as default initial value.

What is Lifetime of a Variable ?

  • Lifetime of a variable is the time till the variable is available.

Types of Storage Class

In C Programming there are following four types of storage classes in c.

1. Automatic variables and Auto storage class.

Scope: Local to the function body they are defined in.

Default value: Garbage value (Random value).

Lifetime: Till the function block they are defined.

If a variable is defined without any storage class specification is by default an automatic variable.

Example code:

#include <stdio.h>

int main()

{

    int a;

 // here a is an automatic variable which is defined within the main function. These are same as local variables

    printf(“The integer is: %d\n”, a);

 // we have not initialized the variable explicitly hence will print a garbage value

return 0;

}

output

The integer is: 0  //garbage value 0

2. External variables and External storage class.

These are same as global variables.

Scope: Global to the program they are defined in.

Default initial value: 0.

Lifetime: These are defined outside a function and can be used throughout the lifetime of the program.

Example Code

#include <stdio.h>

int a; 

// a is a global variable defined outside the main function, a is an external variable

int main()

{

a=22;

printf(“The integer is: %d\n”, a);

return 0;

}

Output

The integer is: 22 // we don’t need to define the variable again and again

3. Static variables and Static storage class.

Scope: Local to the block they are defined in.

Default initial value: 0.

Lifetime: They are available throughout the lifetime of the program.

A static variable exists for use within the function for entire program run.

Example code:

int myfunc()

{

static int sum; // static variable

sum=2+3;

sum++;

return sum;

}

int main()

{

int newvar=myfunc();

printf(“The new varibale is: %d\n”,newvar);

return 0;

}

Output

The new variable is: 6

4. Register variables and Register storage class.

Scope: Local to the function they are defined in.

Default initial value: Garbage value.

Lifetime: They are available till the end of the function block, in which the variable is defined.

Register variables are stored in the CPU register instead of the memory to have faster access.

Example of Register Class

Following program is an example of Register class

#include <stdio.h>

int myfunc()

{

static int sum; // static variable

sum=276+897;

sum++;

return sum;

}

int main()

{

register int newvar=myfunc(); // register variable stored in CPU register

printf(“The new varibale is: %d\n”,newvar);

return 0;

}

Output

The new variable is: 1174

Conclusion and Summary

Today in this tutorial we have discussed the overview of various types of storage classes in C , how many storage class in c , various types of storage class , scope , lifetime and default value of each type of class is also discussed.

Dear students I hope this tutorial will be beneficial for you and computer science students. If you have any query then you may ask in comment section.

Don’t Stop Learning…!

Leave a Reply

Your email address will not be published. Required fields are marked *