In computer programming, storage class is a code attribute used to determine the lifetime and visibility of a variable. There are four types of storage classes in C programming language, namely, automatic, static, register, and extern. Each storage class has a different behavior, and they are used to manage the memory allocation, duration, and scope of a variable. In this article, we will explore each storage class in detail with code examples.
- Automatic Storage Class
The automatic storage class is used to define variables that have a local scope and are automatically destroyed when the function in which they are defined terminates. The variables declared with this storage class are created every time the function is called and are destroyed when the function returns. They cannot be accessed outside the function, and their values are not retained during multiple function calls.
Example:
#include<stdio.h>
int main()
{
int x = 5; // automatic storage class
auto int y = 10; // equivalent to int y = 10;
printf("%d %d", x, y);
return 0;
}
- Static Storage Class
The static storage class is used to define variables that have a global or local scope but retain their values between multiple function calls. The variables declared with this storage class are created only once and are retained until the program terminates. They cannot be accessed from other functions, but they retain their values during multiple function calls.
Example:
#include<stdio.h>
void test()
{
static int count = 0; // static storage class
count++;
printf("%d
", count);
}
int main()
{
test();
test();
test();
return 0;
}
Output:
1
2
3
- Register Storage Class
The register storage class is used to define variables that are stored in registers instead of memory. The variables declared with this storage class have a faster access time compared to variables stored in memory. However, the use of this storage class is limited, and the compiler may not always store the variable in a register.
Example:
#include<stdio.h>
void swap(register int *x, register int *y)
{
register int temp; // register storage class
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a = 10, b = 20;
swap(&a, &b);
printf("%d %d", a, b);
return 0;
}
Output:
20 10
- Extern Storage Class
The extern storage class is used to declare variables that are defined in another file. The variables declared with this storage class have a global scope and can be accessed from other files in the program. The extern variables should be defined at least once in one file.
Example:
// File1.c
#include<stdio.h>
extern int num;
int main()
{
printf("%d", num); // accessing extern variable defined in File2.c
return 0;
}
// File2.c
int num = 10;
Output:
10
In conclusion, storage classes are an essential aspect of programming languages that help manage the memory allocation, duration, and scope of variables. Each storage class has its behavior and is used in specific scenarios. Understanding the behavior of each storage class is important for writing efficient and error-free code.
I will provide further details on the previous topics.
Automatic Storage Class:
The automatic storage class is the most commonly used storage class in C programming. All variables declared inside a function without any storage class specifier are considered to be of automatic storage class. The variables declared with the automatic storage class are created every time the function is called and are destroyed when the function returns. The lifetime of an automatic variable is limited within the block defined in the function, i.e., it cannot be accessed outside the function.
Static Storage Class:
The static storage class is used to define variables that retain their values between multiple function calls. The variables declared with the static storage class are created only once and are retained until the program terminates. The static variables are initialized to zero by default, and we can initialize them to any value during the declaration. The static variables are created in the data segment of the program. The static variables declared inside a function have a local scope but a global lifetime, i.e., they cannot be accessed outside the function, but their values are retained even after the function returns.
Register Storage Class:
The register storage class is used to define variables that are stored in the register of the CPU instead of memory. The variables declared with the register storage class have a faster access time than the variables stored in memory. However, the use of this storage class is limited, and the compiler may not always store the variable in a register due to the availability of registers. We can declare a variable of register storage class only for variables that are going to be accessed frequently or variables that require fast access.
Extern Storage Class:
The extern storage class is used to access variables defined in another file or function. The variables declared with the extern storage class have a global scope and can be accessed from other files in the program. The extern variables should be defined at least once in one file. We need to use the extern keyword whenever we want to use a variable defined in another file. In the absence of the extern keyword, the variable will be considered to be defined in the same file.
In conclusion, the knowledge of storage classes can help us develop more efficient and optimized programs. By understanding how to use the various storage classes, we can simplify our code structure and speed up our program's execution time. It is important to choose the right storage class depending upon the coding scenario and the specific requirements of the program.
Popular questions
-
What is the difference between automatic and static storage classes?
Answer: The automatic storage class is used to define variables that have a local scope and are automatically destroyed when the function in which they are defined terminates, while the static storage class is used to define variables that retain their values between multiple function calls. Static variables are initialized to zero by default and are retained until the program terminates. -
What is the purpose of the register storage class?
Answer: The register storage class is used to define variables that are stored in the register of the CPU instead of memory. The variables declared with the register storage class have a faster access time than the variables stored in memory. We can declare a variable of register storage class only for variables that are going to be accessed frequently or variables that require fast access. -
Can an automatic variable be accessed outside the function where it is defined?
Answer: No, an automatic variable cannot be accessed outside the function where it is defined. The lifetime of an automatic variable is limited within the block defined in the function. -
How is the lifetime of a static variable different from that of an automatic variable?
Answer: The static variables declared inside a function have a local scope but a global lifetime, i.e., they cannot be accessed outside the function, but their values are retained even after the function returns. The lifetime of a static variable is from the time the program starts to the time it ends. -
What is the purpose of the extern storage class?
Answer: The extern storage class is used to access variables defined in another file or function. The variables declared with the extern storage class have a global scope and can be accessed from other files in the program. The extern variables should be defined at least once in one file. We can use the extern keyword whenever we want to use a variable defined in another file.
Tag
CODESTORAGE