Storage classes in C
a storage class specifies the scope, lifetime, and linkage of a variable or function. In this article we are going to explore storage classes in C programming. This article have explained different storage classes with programming examples.
The storage class of a variable or function determines where and how the variable or function is stored in memory and how other parts of the program can access it.The storage classes in C language are stack, heaps, and the main memory. Each has different characteristics and applications. Understanding how to use the storage classes in C gives a programmer maximum control over his system’s memory usage. Plus, implementing a well-thought-out memory management scheme can improve a program’s performance and stability. In this article we are going to explore storage classes in C programming.
Table of contents
Best-suited C / C++ courses for you
Learn C / C++ with these high-rated online courses
What are storage classes?
A storage class is a type of attribute that specifies the location and lifecycle of a variable or function. In other words, a storage class describes the characteristics of a variable or function.
There are four storage classes in C:
- Auto: This is the default storage class for all local variables. Variables declared within a function without any storage class specifier automatically allocate memory on the stack and have a local scope. Their lifetime is limited to the duration of the function call.
- Register: Variables declared with the register storage class are stored in CPU registers. This can improve the program’s performance, as access to registers is faster than memory access. However, the number of registers is limited, so the register should be used judiciously. Variables declared with register have the same scope and lifetime as auto variables.
- Static: Variables declared with the static storage class are allocated memory in the data segment of the program and retain their value between function calls. They have a local scope, but their lifetime is the entire duration of the program. Functions can also be declared as static, and these functions can only be called within the same source code file where it is defined.
- Extern: Variables declared with the extern storage class are defined in one source code file and can be used in other source code files. They are typically used to share global variables between multiple source code files. The variable needs to be defined (not just declared) in one source file with the keyword extern and in other files where they’re used.
It’s worth noting that C also has _Thread_local storage class, which denotes thread-local storage duration for variables.
Also read: Difference Between C and C++
Preparing for a C++ interview? Check out the Top C++ Interview Questions and Answers
Also Read – Understanding Operators in C++
Learn more: Basics of C Programming Language
Storage classes in C with examples
1. Auto storage class
The auto storage class in C is the default storage class for all local variables. If you declare a variable within a function without any storage class specifier, the variable will be treated as an auto variable.
The memory for auto variables is allocated on the stack, and the variable’s lifetime is limited to the duration of the function call. The scope of an auto variable is also limited to the block in which it is defined.
Parameter | Value |
Keyword used | Auto |
Lifetime | Throughout the program |
Declared at | Beginning of the function |
Default value | Garbage value |
Example
void myFunction() { int x; // x is an auto variable x = 10; printf("%d", x);
In the above example, the variable x is an auto variable, and its memory is allocated to the stack. The lifetime of x is limited to the duration of the function call, and the scope of x is limited to the myFunction() block.
Due to the automatic memory management of the stack, variables are automatically deallocated when the execution flow leaves their scope, so no additional step is required to deallocate the memory.
2. Static storage class
A static storage class is a type of storage class in C and C++ that defines a variable with a static lifetime. This means that the variable is allocated in memory when the program starts and is only deallocated once the program ends. It also means that the variable retains its value between function calls.
Parameter | Value |
Keyword used | Static |
Lifetime | Throughout the program |
Declared at | Inside the function cannot be accessed outside the function. |
Default value | Zero |
Example
static int x = 0;
#include <stdio.h>int myfunc() {static int sum =4 ; sum++;return sum; }void main(){printf("sum = %d \n", myfunc());printf("sum = %d \n", myfunc()); }
Output
sum = 5
sum = 6
The myfunc() function has a variable called sum, initially set to 4. The static keyword is used to create a static variable, meaning that the variable’s value will be preserved between function calls.
The myfunc() function increments the value of the sum by 1 and then returns the new value. When the myfunc() function is called, it will always return the next value in the sequence (4, 5)
In the main function, myfunc() is called twice, and the return value is printed using the printf function.
The first time myfunc() is called, it returns the initial value of the sum, which is 4. The second time it’s called, it increments the sum to 5 and returns that value.
So, here also, the Function myfunc returns the value of sum by increasing it everytime, and the main function is just calling the function and printing the returned value.
Related Read – Binary Search in C++
3. Register storage class
A register storage class is a type of storage class in C and C++ that defines a variable that is stored in a register instead of in memory. This can potentially improve the performance of the program, as accessing a register is generally faster than accessing memory.
Parameter | Value |
Keyword used | register |
Lifetime | within the function block |
Default value | Garbage value |
In C and C++, variables with a register storage class are defined by using the register keyword. Here is an example of a variable with a register storage class in C:
register int x = 0;
So put the register keyword in front of the variable declaration.
It’s worth noting that the use of the register keyword is only a request to the compiler to store the variable in a register. The compiler is free to ignore the request if it determines that it is not beneficial for performance. And also, there are a limited number of registers available for the register storage class, so the variable might not be stored in a register in case all the registers are being utilized.
Example
#include <stdio.h>void main() { register int x; for (x = 0; x < 10; x++) { printf("%d ", x); }}
In this example, the variable x is defined as a register variable and is used as the loop counter in a for a loop. Because x is defined as a register variable, it will be stored in a register, and the program should run faster as it will not be required to access memory as often.
4. Extern storage class
The extern storage class is used in C and C++ to declare a variable defined in another source file or to indicate that a function is implemented externally to the current source file.
Parameter | Value |
Keyword used | Extern |
Lifetime | Throughout the program |
Declared at | Declared outside the functions before the main function. |
Default value | Zero |
It is important to note that when you define a variable or function with the “extern” storage class, you do not include an initializer. The storage for the variable or function is defined in another file, and the initializer for the variable or function should be included in that file.
Explore: C Programming Online Courses & Certifications
Explore: Free C Programming Courses Online
Example
File 1
#include <stdio.h> int x; void print_x() { printf("x = %d\n"
File 2
#include <stdio.h> extern int x;extern void print_x(); int main() { x = 5; print_x(); return 0;}
In this example, “x” is defined in File 1 and is declared with the “extern” storage class in File 2. The “extern” declaration in File 2 tells the compiler that “x” is defined in another file, so it should not allocate storage. Instead, the compiler generates a symbol for “x” so that it can be accessed in File 2. The function “print_x” is also defined in File 1 and is declared with the “extern” storage class in File 2.
The variables or functions declared as extern are always global and are accessible from any file once included.
Conclusion
Overall, this article provides a solid introduction to storage classes in C programming and includes helpful examples to illustrate key concepts.We you liked this article please do like it as well as share it with your friends.
This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio