Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: C Programming. 1 Comment on Storage Classes in C
0
(0)

The storage classes in C specify the scope and lifetime of a variable in a C program. The scope (active) specifies in which parts of the program is the variable accessible and the lifetime (alive) specifies how long a variable is available in the memory so that the program will be able to access that variable.

 

There are four storage classes in C. They are:

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

 

The storage classes’ auto, register, and static can be applied to local variables and the storage classes’ extern, and static can be applied to global variables.

 

auto

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


When a variable is declared with the storage class auto, the variable’s scope is within the function or block in which it is declared and the lifetime is until the function or block in which it is declared completes.

 

Syntax for declaring auto variable is shown below:

 

auto-syntax

 

In any program, if a local variable is declared without any storage class then it is automatically set to auto storage class.

 

register

 

When a variable is declared with the storage class register, the variable will be stored inside one of the registers of the CPU. The registers are under the direct control of CPU. So, data inside the register can be processed at a faster rate than the data that resides in the main memory.

 

For a program to execute faster, it is always best to store the most frequently used data inside register. The scope and lifetime of a register variable is same as that of a auto variable. Syntax for declaring a register variable is as shown below:

 

register-syntax

 

extern

 

The extern storage class specifies that the variable is declared in some part of the program. Generally this storage class is used to refer global variables in a program. Note that extern variables cannot be initialized.

 

The scope of a extern variable is throughout the entire program and the lifetime is until the program completes its execution.

 

In a multi-file program, a global variable in one file can be accessed from another file by using the storage class extern. Syntax for declaring a extern variable is as shown below:

 

extern-syntax

 

static

 

The static storage class can be applied to both local variables and global variables. The static local variables are accessible only within the function or block in which they are declared, but their lifetime is throughout the program.

 

The static global variables are accessible throughout the file in which they are declared but not in other files. Syntax for declaring static variable is shown below:

 

static-syntax

 

The four storage classes can be summarized as shown below:

 

Storage Class Declaration

Location

Scope

(Visibility)

Lifetime

(Alive)

auto Inside a function/block Within the function/block Until the function/block completes
register Inside a function/block Within the function/block Until the function/block completes
extern Outside all functions Entire file plus other files where the variable is declared as extern Until the program terminates
static

(local)

Inside a function/block Within the function/block Until the program terminates
static

(global)

Outside all functions Entire file in which it is declared Until the program terminates

 

Note: The extern variables cannot be initialized. The default value for static variables is zero.

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

This is really useful, thanks.

Leave a Reply

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