Use of Storage Class in C
In C, storage classes are used to control the behaviour of variables in terms of their scope, lifetime, and memory allocation.
-
Storage Classes are used for variables that are temporary and needed only within a specific function. They are automatically created when the function is called and destroyed when it exits.
-
Static variables are used when you want a variable to retain its value between function calls. They are initialized only once and can be accessed across multiple calls to the same function.
-
The register variables are used for fast access to frequently used values. However, the compiler may ignore the request to store a variable in a CPU register if there are too many or if it doesn't provide a performance benefit.
-
External variables are useful when you need to share a variable across multiple source files. Define it in one file and declare it as an extern in other files that need to access its value.
Also read-Bit stuffing program in c
Purpose of storage class in C
Storage classes in C serve several crucial purposes. They allow programmers to tailor the behaviour of variables within their programs.
For instance, auto variables are temporary and ideal for short-lived calculations within a function, while static variables retain values between function calls. Register variables optimize performance by storing data in CPU registers when possible. Extern variables facilitate sharing data across multiple source files. By using the appropriate storage class, developers can efficiently manage memory usage, control variable access, and enhance program performance, making C a versatile language for various programming tasks.
Types of Storage Classes in C
We have four different storage classes in a C program:
- auto
- register
- static
-
extern
Let’s see each storage class briefly and understand it better using different code snippets.
Must Read Passing Arrays to Function in C and Floyd's Triangle in C
1. Automatic Storage Class
Automatic variables/auto are defined inside a function. A variable declared inside a function without a storage class name is by default considered as an auto variable.
Syntax: auto <datatype> variable;
The features of Automatic variables are:
- Storage : Memory
- Initial Value : Garbage or Unpredictable
- Scope : Within Function
-
Lifetime : Till the control remains in the function
These variables are created when the function is called and destroyed automatically when the function is exited.
Automatic variables are local to the function in which they are declared. Therefore, these values cannot be accessed by any other function. The keyword used is “auto.”
Code:
#include<stdio.h>
int main( )
{
auto int a =100; //Auto variable one
{
auto int a = 300; //Auto variable two
{
auto int a = 500; //Auto variable three
printf ("a=%d\n",a); //Prints what is within block
}
printf ("a=%d\n",a); //Prints what is within block
}
printf ("a=%d\n", a); //Prints what is within block
return 0;
}
Output:
a=500
a=300
a=100
As Automatic variables are permitted within the block, the values of variables inside a particular block have been printed.
You can also read about the jump statement and Short int in C Programming
2. Extern Storage Class
External variables/extern are also known as global variables. These are declared outside the function, and the values of these variables are available to all the program’s functions.
syntax : extern <datatype> variable (extern is optional as you declare it globally.)
Unlike Local Variables, Global Variables can be accessed by any function in the program. If the same name is given to both the global and local variables, priority is given to the local variable. The keyword “extern” is used to declare these variables.
The features of external variables are:
- Storage: memory
- Initial value: zero
- Scope: Global
-
Lifetime: Till the program comes to an end
Code:
#include<stdio.h>
int a=20; //Global variable
int main( )
{
fun1( );
fun2( );
fun3( );
printf("\n In main function a=%d", a);
return 0;
}
void fun1( )
{
printf("\n In fun1 a = %d", a); //prints the global value
}
void fun2( )
{
int a = 10;
printf("\n In fun2 a = %d",a); //prints 10
}
void fun3( )
{
printf("\n In fun3 a = %d", a); //prints global value
}
Output:
In fun1 a = 20
In fun2 a = 10
In fun3 a = 20
In main function a=20
In this program, local variables and global variables are declared with the same name in fun2( ). In this case, when fun2( ) is called, the local variable “a” of fun2( ) overrides the global variable “a.”
3. Static Storage Class
Static variables/static may be Local or global depending upon where it is declared. For example, it is static global if it is declared outside the function; otherwise, it is static local if declared inside a function block.
Syntax: static <datatype> variable;
A static variable is initialized only once and can never be re-initialised. The value of the static variable persists at each call, and the last change made in the variable remains throughout the program execution. The keyword used to declare these variables is “static.”
The features of a static variable are:
- Storage: memory
- Initial value: zero
- Scope: Local to the block in which the variable is defined
-
Lifetime: persists till the end of program execution
Code:
#include<stdio.h>
void incr( )
{
static int x; //default value is 1
x=x+1;
printf("%d\n", x);
}
int main( )
{
incr( ); //prints 1
incr( ); //prints 2
incr( ); //prints 3
return 0;
}
Output:
1
2
3
As the static variables store the value, they don’t get initialised to ‘1’ for every function call. So the output increments whenever we call the function from main.
Must Read Static Blocks In Java.
4. Register Storage Class
Instead of storing in memory, variables can also be stored in the register of the CPU. The advantage of storing in registers is that register access is faster than memory access, so frequently accessed variables are kept in registers for faster execution.
Syntax: register int count;
The keyword ‘register’ tells the compiler that the variable list is kept on the CPU registers. If the CPU fails to keep the variables in CPU registers, in that case, the variables are assured as auto and stored in the memory.
Syntax: register <datatype> variable;
Note: CPU registers are limited in number. So, we cannot declare more variables as register variables. The compiler automatically converts the register variables into non-register variables once the limit is reached.
We cannot use the register class for all types of variables. The CPU registers in the microcomputer are 16-bit registers. The data types float and double need space of more than 16 bits. If we define any variable of these types with the register class, no errors will be shown, but the compiler treats them as auto variables.
The features of register variables are:
- Storage: Registers
- Initial value: Garbage
- Scope: Local
-
Lifetime: Until the control remains in that function block
Code:
#include<stdio.h>
int main( )
{
register int i; //declaration of register variable
for (i=1; i<=5; i++)
printf ("%d\n", i); //prints 1,2,3,4,5
return 0;
}
Output:
1
2
3
4
5
The register variables work only in the particular block, so the value doesn’t change until the block expires.
Must Read what is storage class in c and Decision Making in C
Frequently Asked Questions
What is the storage class variable?
A storage class variable is a programming concept defining a variable's scope and lifetime, like its visibility within functions or throughout the program, and how long it exists in memory.
Why storage class memory?
Storage class memory refers to the type of memory management used for variables, which impacts their lifespan and accessibility. It's crucial for optimizing memory usage and controlling variable behaviour.
Why are storage classes less frequently used?
Register storage classes are less commonly used because they are not guaranteed to be stored in a register. Depending on the available registers and the other variables in the programme, the compiler may decide to store the variable in memory instead.
What are storage classes used to provide?
Storage classes are used to provide information about the scope, lifetime, and linkage of variables and functions. A variable's or function's scope is the area of the programme where it can be used. The lifetime of a variable is the period of time during which it exists. The linkage of a variable or function is its ability to be accessed from other files.
Which storage class is faster in C?
The fastest storage class in C is the register storage class. Variables declared with the register storage class are stored in registers, which are fast memory locations on the CPU. This makes access to these variables much faster.
Why are storage classes important for variables?
Variable storage classes are important because they determine how the compiler will treat them. The scope of a variable determines where it can be used, the lifetime of a variable determines how long it will exist, and the linkage of a variable determines if it can be accessed from other files.
Conclusion
This blog discussed the storage classes and their types, followed by exploring the types of variables in storage classes and analyzing them with code. At last, we have discussed some of the faqs.
Explore more related articles:
You can refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.