Javatpoint Logo
Javatpoint Logo

What is a Storage Class in C?

We declare variables in almost every program. Not every variable has the same features. The declarations, accessibility in different parts of the program varies from variable to variable depending on the location the variable is declared. "Storage classes" simply are used to determine some important features of a variable like

  1. Visibility
  2. Memory location
  3. Initial value if not initialized
  4. Life time

This article explains what storage classes are in C language. So, there are 4 different storage classes in C programming language:

  1. Automatic (auto)
  2. External (extern)
  3. Static (static)
  4. Register (register)

Here, the words in the braces are the keywords used for that particular storage class. Here is a simple table which we will explore further with examples:

Storage class keyword Where is the variable stored Initial value of the variable Scope Lifetime
auto Stack garbage Within the code block it is used End of the code block
extern Data segment 0 Global( Active across multiple files) Till the end of the program
static Data segment 0 Within the block of the code Till the end of the program
register CPU register garbage Within the block of code End of the code block

The four features of every variable:

  1. Scope of the variable: The scope of a variable refers to the parts of the program till which the value of the variable can be accessed.
  2. Lifetime of the variable: This determines how long the variable will stay alive across the whole program.
  3. Initial value: The value a compiler gives to a variable when we just declare it without giving it any value.
  4. Storage: Determines where the variable is stored.

Explanation

1. "Automatic" storage class

This is the default storage class for all variables. Hence, we won't mention it in the declaration separately. These variables can only be accessed within the block of code where these are declared. These variables can be accessed inside the nested blocks if any.

If we want to access the variable outside its block of code, then we use pointers that point to that variable and access using *pointer.

We use the keyword "auto" to access an automatic variable. If not initialized, the compiler gives the variable a garbage value which we can modify later in the program.

Quick view:

Storage: Stack in the processor

Lifetime: In that particular block

Scope: Permitted to that particular block

Initial value: garbage value

Syntax:

Example program:

Output:

num=500
num=300
num=100

Understanding:

Different code blocks in the program are differentiated with different blocks to make it clear to understand. The scope of the given values to the automatic variables is permitted to that specific block only. Hence, on execution, first the inner block of code printed the value of num as 500. After that, the outer block printed the value of num as 300, and then the outer most block of code is executed as the value is printed as 100.

2. "External" storage class

Simply put, an extern variable is a global variable. It can be accessed anywhere in the program irrespective of where it is declared-in different block of code, also in a function declaration. The initialized value can be modified anywhere around the program. All we have to do is to place the "extern" keyword before the variable in the declaration. Basically, the value of an extern variable is initialized in the starting of the program and it is used elsewhere in the program. Using extern keyword extends the visibility and lifetime of that particular variable.

Purpose: The main purpose of using this external variable is they are accessed between two different files that are a part of a huge program with much readability without ambiguity.

  • We don't need to use extern keyword manually when we want to declare an extern variable inside a function declaration or definition because, it is taken implicitly because of the visibility of the functions throughout the program.

Quick view:

Storage: In the data segment (Processor)

Lifetime: Till the end of the program

Scope: Global (Alive in multiple files)

Initial value: Zero (0)

Syntax:

Example program:

Output:

0

Understanding: The extern variable is declared above the main function. It is now available everywhere in the program-globally. It is not given any value-not initialized. Now, we printed the variable only to see its initial value given by the compiler as 0.

Output:

num = 7

What is the difference between a global variable and an extern variable?

  • Simply said, when we use the keyword extern, it doesn't create a variable that is going to be available globally. Instead, we are accessing a global variable that is defined elsewhere in other module.
  • By using the extern keyword before a variable, we are telling the compiler that there is this variable defined in another module. Don't allocate separate storage for it, just find it and access it from that module to use in this program.

3. "static" storage class

A static variable is known for its preserving feature. It stores the value even after it is out of the scope till the end of the program. It keeps the last modified value in its storage. We don't need to keep declaring it to use it rather it is re-declared.

Quick view:

Storage: In the data segment (Processor)

Lifetime: Till the end of the program

Scope: Within the local block

Initial value: Zero (0)

Syntax:

Example program:

Output:

6  11

Understanding:

The variable num is declared as a static variable, given a value 1 in the function declaration block. So, the scope of that variable is permitted to that block only. But, as you can observe, during the first function call, the value is added up with 5 and became 1+5 = 6. It is printed and during the second function call, the value of num is taken as 6 hence it became 6+5 = 11. It is not taken as 1 as a static variable preserve the last modified value in them.

4. "register" storage class

These variables are the same as the automatic variables in terms of the functionality. The only difference is in the storage. Automatic variables are stored in the stack but register variables are stored in the register of the microprocessor if a free register is available. This makes these variables much faster to access and work with than automatic variables.

Points to remember:

  • If a free register is not available at that moment in the microprocessor, then they are stored in the normal memory only.
  • The variables which are frequently needed in the program are declared as register storage class variables to decrease the running time of the variable.
  • We cannot obtain the address of a register variable using pointers.

Quick View:

Storage: register class in the microprocessor

Lifetime: In that particular block

Scope: Permitted to that particular block

Initial value: garbage value

Syntax:

Example program:

Output:

Compiler Error

Understanding:

The address of this variable won't be available in the processor's stack. It will be available in the register class. So, the compiler will not be able to access its address &num, hence raises an error.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA