Storage Classes in C language – Auto, Register, Static, and Extern

storage-classes-in-c-language-with-example-programs

Storage Classes in C Introduction:

We have looked at the Call by value and call by reference in our earlier posts, In today’s article, We are going to look at the Storage Classes in the C programming language.

Storage Classes in C Programming Language:

So far we have declared the variables by specifying the datatype. But variables also have another attribute, which defines a few key characteristics of the variable, That attribute is Storage Class. The Storage class in C decides where the variable will be stored in RAM and how long it will live, What is the scope of the variables, etc. The knowledge of storage is very valuable once we start writing multi-file programs (applications).

The storage class decides the following characteristics of the variable

  1. Storage Location – Where the variable will be stored ( Stack or Register, etc )?
  2. Lifetime – How long the variable will live?
  3. Scope ( Visibility ) – Where will be the variable available to access (use)?
  4. Default Value or Initial Value – What is the initial value of the variable, if declared without any initial value?

Before going into further details, Here is the syntax of variable declaration with storage class

We have four storage classes in the C Programming language, They are

  1. auto storage class
  2. register storage class
  3. static storage class
  4. extern storage class

Let’s look at each one of the storage classes in detail.

The auto storage class in C language with Example Programs:

If no storage class is specified, The variables which are defined within the function or code block are called the auto storage class ( Automatic Storage class) variable.

syntax:

auto datatype variable_name;

Now let’s look at the four characteristics of the Automatic storage class variables.

Storage Location:

The automatic storage class variables will be stored in the stack segment of the RAM.

Lifetime:

The auto variables will be created when the function or block started and will be destroyed once the function/block is returned. So Lifetime of the automatic storage class variable is the same as the function.

Scope ( Visibility ):

The scope of the automatic variables is within the function or code block which are declared. The auto variables won’t be visible outside of the function/block that they declared, So we can’t access them from outside of the function/block.

Default Value or Initial Value:

The Default Value of the Automatic variables is Garbage Value or Undefined Value. So it is a good idea to initialize the variables with the appropriate value.

📢So The default storage class of the variables declared within the function or code block is Automatic storage calls.

Here are the properties of the Automatic Storage class variable in C language:

Storage LocationLifetimeScopeDefault Value
StackFunction/BlockFunction/blockGarbage/undefined value

Program to understand Automatic storage class in C Language:

Here is a program with automatic storage class variables.

Program Output:

Compile and run the program.

auto-storage-class-in-c-language

Auto Storage class program Explanation:

In the above program, We have created the three automatic variables with the name a. and they are defined in different code blocks so no compilation error.

They are three different automatic variables ( with three different scopes )

  • The func1() scope
    • The scope of the automatic variable a is func1, So when we printed the variable a inside of the func1() function, So we got the value 10.
    • The variable a life also depends on the func1() life, The a will be created when func1 is created and destroyed once the func1() is returned.
  • Code block scope within the main function
    • The variable a ( auto int a = 20;) which is inside of the block will be visible only within the block., So When we printed the value of the variable a from the code block within the main() function, The auto variable inside of block ( auto int a = 20) will shadow the outer main function auto variable( i.e auto int a = 5). So we got the number 20 as the output.
    • Once the code block is finished, the auto variable a will be destroyed.
  • The main function scope
    • Similarly, When we printed the value of the main function variable a, We got the value 5 ( as auto int a = 5 is visible in the main function scope)

We have properly initialized our automatic variables in the above example, We can try without initializing the variables, We will get the undefined values or garbage values.

As the automatic variables are visible only inside the declared function/block, We can re-use the variable names in other functions/blocks.

Register Storage Class in C Language:

The Register storage class variables can be declared within the Function/block. To create a register variable, use the register keyword like below.

The register variables are very similar to the automatic variables, except the register variables are stored in CPU registers instead of the stack.

Lifetime:

The register variables will be created when the function started and will be destroyed once the function is returned. So Lifetime of the register storage class variable is the same as the function or block they declared.

Scope ( Visibility ):

The scope of the register variables is within the function or code block which are declared.

Default Value or Initial Value:

The Default Value of the register variables is Garbage Value or Undefined Value.

Storage Location:

The register storage class variables will be stored in the CPU registers.

The CPU registers are very close to the CPU core, So they are much faster than the RAM, But they are limited (in Bytes). Accessing the variables stored in CPU registers is very fast compared to accessing it from RAM.

So if there is any variable that is accessed quite frequently, Then we can use the register storage class. ( Loop control variables ) for it.

As specified earlier, The CPU registers are very limited, If the compiler is not able to accommodate the CPU register, Then these register storage class variables will be treated as automatic variables. ( Will be stored in stack segment of RAM )

As we are using the CPU register for register storage class variables, The address of operator (&) won’t work on register variables. So if you try to access the address of the register variable, The compiler will raise an error ( address of register variable ‘i’ requested).

Register Storage class variable Properties in C:

Storage LocationLifetimeScopeDefault Value
CPU RegistersFunction/BlockFunction/blockGarbage/undefined value

Let’s look at the examples of register storage class variables.

C Program to understand the Register Storage class:

Program Output:

Register-storage-class-in-C-programming-language

If you see the above program, We have created a register storage class variable i. And then we used the i as the for loop counter. We have created a loop that starts from and goes till 10000000. So the variable i will be accessed quite frequently. So the compiler can place the variable i in the CPU register and access it very fastly.

Register storage class with the address of operator (&):

If we change the above program and try to access the address of the register variable i, Then we will get the compilation error – error: address of register variable ‘i’ requested

Program Output:

Static Storage Class in C Language:

The variables which are declared with the static storage specifier are called static variables. The static storage class can be used for functions or variables.

Syntax:

static datatype variable_name;

The local static variables are stored in the data segment of the RAM. ( The initialized static variables are stored in the data segment ( .bs) and the uninitialized variables are stored in the .bss segment)

There are two types of static variables.

  1. Local Static variables
  2. Global static variables (and functions)

Local Static Variables in C Language:

The variables which are declared within the function or block are called local static variables. These variables are only visible inside the declared function, But the local static variable will live throughout the program. This means, Even though the function (where variable declared) is returned, The static variable won’t be destroyed.

Let’s look at the characteristics of Local Static Variable

Storage Location:

The local static variables are stored in the data segment of the RAM. ( The initialized static variables are stored in data segment ( .bs) and the uninitialized variables are stored in .bss segment)

Lifetime:

The Lifetime of static local variables is the same as the program. The static variable is created when the program begins and will live throughout the program and be deallocated when the program is terminated. The local static variable will not be created and destroyed based on the function calls. The local static variable is created only once at the start of the program and will retain it’s value between the function calls.

Scope ( Visibility ):

The scope of the local static variable is function or block where it is declared. So it won’t visible outside of the function/block.

Default Value or Initial Value:

If the static variable is not initialized, Then the default value of the local static variable is Zero. ( All .bss variables will be initialized with zeros during the program startup)

Properties of Local Static Variable in C:

Storage LocationLifetimeScopeDefault Value
Data Segment ( .ds / .bss)ProgramFunction/blockZero ( 0 )

Let’s look at couple of programs on Local static variables.

Program to understand Local static variables within a function:

Program Output:

Local-Static-storage-class-variables

As you can see from the above output, We have two local static variables here. One in the scope of the main function and another one in function1’s scope. This will clarify that, Local static variables when declared within a function are function scoped and not visible outside of the function.

Local Static Variables Lives throughout the program:

Here is a C Program to demonstrate that The Local static variable’s lifetime is equal to the program’s lifetime.

Program Output:

Local-Static-storage-class-variables-in-C-program

From the above program output, We can confirm that the local static variable i in the function boost is able to retain its value between the function calls.

So when the program started, The boost function’s variable i is created and got the value 50 (as we initialized with 50). So when we called the boost function multiple times, The value of i is increased by 10 for every call.

  • In the first call to boost function, The value of i is 50, and we printed it on the console and then incremented the value by 10, Now the i value became 60.
  • When we made the second call, The variable i won’t be created again, as static variables live throughout the life of the program, So i will be available and it will have the latest value 60. So when we printed the i, We got 60. Finally, we incremented the i value by 10. Now variable i became 70.
  • Similarly, for the Third and fourth calls, We printed the i value and incremented it again, So we got the values as 70 and 80.

With the above example, We can say

  • Static variable life is throughout the program.
  • Static variable is able to retain its value between the function calls. As the static variable is not created and destroyed for every function call/return.

Global Static Variables in C Language:

The global variables or functions which are defined with the static storage class specifiers are called as the global static variables in C language.

Global static storage class variables syntax in C:

Similar to local static variables, The global static variables also will live throughout the program. and will have the same initial value and storage location, but they differ in scope. While the local static variables are only visible in the declared function or block. The global static variables are visible to all functions in the file. But they won’t be visible outside of the declared file.

The global static variables have the Internal Linkage and It is file scoped, So the variables and functions which are declared using the internal linkage will be visible in the declared file only. As the global static variable has internal linking, You can’t access them from the other files.

📢 We can create the file-scoped variables using the global static storage class variables.

Let’s briefly look at the characteristics of global static variables.

Storage Location:

The global static variables are stored in the data segment of the RAM. ( The initialized static variables are stored in the data segment ( .bs) and the uninitialized static variables are stored in .bss segment)

Lifetime:

The Lifetime of global static variables is the same as the program. The global static variables are created when the program begins and will live throughout the life of the program and are deallocated when the program is terminated.

Scope ( Visibility ):

The global static variables are file scoped, so they will be visible to all functions in the file.

Default Value or Initial Value:

If the static variable is not initialized, Then the default value of the local static variable is Zero. ( All .bss variables will be initialized with zeros during the program startup)

Linkage:

Global static variables have the Internal Linkage.

Global Static storage calls variables Properties:

Storage LocationLifetimeScopeDefault Value
Data Segment ( .ds / .bss)ProgramFileZero ( 0 )

📢 So when you want to have a global variable or function, which only needs to be visible in the declared file, Then you can use the global static variables, This way we can hide our function or variables from outside files.

Program to understand the global static storage class variable in C:

Program Output:

Global-Static-storage-class-variables

As we can see from the above output, The global static variable g is visible throughout the file, So The update function and main function is able to access and modify the variable g.

Extern Storage Class in C Programming Language:

The variables which are defined/declared are called as the Extern storage class specifier. We can use the extern storage class for the variables and functions.

Syntax of Extern Storage class variable in C

We can access the extern storage class variables and functions from anywhere in the program. which means we declare the variable/function in one file and can be accessed from another file.

Let’s look at the properties of extern storage class variables or functions

Storage Location:

The extern storage class variables are stored in the data segment of the RAM. ( The initialized extern variables are stored in the data segment ( .bs) and the uninitialized extern variables are stored in .bss segment)

Lifetime:

The Lifetime of extern variables is the same as the program. These variables are created when the program begins and will live throughout the life of the program and are deallocated when the program is terminated.

Scope ( Visibility ):

The extern storage class variables or functions will be visible to all files. Can be accessed from any function file in the program. The extern variables are program-scoped (or Application Scoped / Global Scoped).

Default Value or Initial Value:

If the extern storage class variable is not initialized, Then the default value of the variable is Zero. ( All .bss variables will be initialized with zeros during the program startup)

Linkage:

The extern variables have the External Linkage.

📢 External Linkage and Global Scope – This means the variables and functions which have external linkage will be accessed to all source files in the program (application). So can be accessed from any file of your program

Before looking at an example of extern storage class variables, We need to understand the difference between the declaration and definition of variables.

Extern storage class variable Declaration:

As we can access the extern variables or functions from another file as well. We can do that by specifying the declaration using the extern keyword like below.

The above statement extern int i; is the declaration of variable i. This declaration won’t create the variable, This declaration simply informs the compiler, There will be a variable named i with the integer datatype, which is going to be defined somewhere in the program, that might be within the present source file or another file.

This extern variable declaration is very much similar to the function declaration or forward declaration of the function. We are letting the compiler know that there will be a function or variable, which is going to be initialized somewhere in the program, So we can access that variable or call that function without causing any compilation errors.

📢The extern variable declaration won’t cause any memory allocation. We can declare the variables multiple times in the program using extern keyword.

Now we declared the extern variable, We need to define it somewhere in the program. Let’s say we have another file called utils.c, Where we defined the variable i

utils.c

Extern Storage class Variable Properties:

Storage LocationLifetimeScopeDefault Value
Data Segment ( .ds / .bss)ProgramProgram (Application)Zero ( 0 )

Program to understand Extern Storage Class in C:

Let’s create a file named utils.c and define a couple of extern variables i and j. We also create a sum function as well.

utils.c

Create another file named main.c, which is going to use the above utils.c files variables and functions.

main.c

As you can see we have declared the variables i and j using the extern keyword, Similarly we also declared the sum function. Here we are telling the compiler that, There are two variables i and j, and function sum are defined somewhere in the program (might be in this file or another source file), So by using this declaration we are conveying to the compiler the properties of the extern variables(datatype) and functions (return value, arguments).

Run the above program using GCC compiler using the following command

extern-storage-class-in-c-language-with-example-program

As you can see from the above output, The main function from main.c file is able to access the variable values( i, j) , which are defined in the utils.c file.

Similarly, The main function is also able to call the sum function, which is declared in the utils.c file.

This is possible due to the extern storage class variables and functions. As they can be accessed from anywhere in the program.

📢If you don’t specify any storage class for global variables and functions, Then they will have the external linkage and can be accessed from all the files in the program.

Library functions storage class : The printf() and scanf() functions storage class:

If you look at the declarations of printf and scanf functions in the stdio.h, You will come to know that they also declared with the extern storage class.

Here is the prototype of the printf function from the stdio.h header file.

That is the reason, We are able to call the printf and scanf and all other library functions as they have external linkage and can be accessed from other files.

📢How to see the prototype of printf function or other library functions? Please go through the following article, Where we looked at the compilation process in C language in detail.

Storage Classes in C Langauge – Conclusion:

We have looked at the Storage Classes in C language like Auto Storage class, Register Storage class, Static Storage class, and Extern storage class variables. How the storage class affects the location, lifetime, scope( visibility), Default value, and Linkage of the variable and how important it is to understand the storage classes to develop multi-file (Source files) applications.

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply