Storage Classes in C Programming
Hello Everyone, In this article, we will cover the concept of Storage Classes in C Programming. Storage classes define the scope, visibility, and lifetime of variables and functions in a C program. Here We will break down each type of storage class—automatic, external, static, and register—so you can see how they work and when to use them with the help of example. Whether you’re new to C or have some experience, this guide will help you to understand Storage Classes in C effectively. Let’s get started.
Storage classes in C programming define the scope (visibility), lifetime (how long the variable exists in memory), and linkage (whether the variable can be accessed from other source files) of variables and functions.
Types of Storage Classes in C
There are four storage classes in C:
- Automatic Storage Class
- External Storage Class
- Static Storage Class
- Register Storage Class
Let’s discuss each storage class with examples:
1. Automatic Storage Class:
- Keyword: auto
- Default storage class for local variables
- Variables have local scope and are destroyed once the function/block they are declared in exits
- Variables have no linkage, meaning they can’t be accessed from other source files
Example:
#include <stdio.h>
void auto_example() {
auto int num = 5; // Declaring an automatic variable
printf("Automatic variable num: %d\\n", num);
}
int main() {
auto_example();
return 0;
}
2. External Storage Class:
- Keyword: extern
- Variables have global scope, meaning they can be accessed from any function
- Variables have external linkage, meaning they can be accessed from other source files
- Variables are initialized to zero if not explicitly initialized
Example:
// file1.c
#include <stdio.h>
extern int global_var; // Declaration of an external variable
void print_global_var() {
printf("External variable global_var: %d\\n", global_var);
}
// file2.c
#include <stdio.h>int global_var = 42; // Definition of the external variable
int main() {
print_global_var();
return 0;
}
3. Static Storage Class:
- Keyword: static
- Variables have local scope but their lifetime is the entire duration of the program
- Variables have internal linkage, meaning they can’t be accessed from other source files
- Variables are initialized to zero if not explicitly initialized
Example:
#include <stdio.h>
void static_example() {
static int counter = 0; // Declaring a static variable
counter++;
printf("Static variable counter: %d\\n", counter);
}
int main() {
for (int i = 0; i < 5; i++) {
static_example();
}
return 0;
}
4. Register Storage Class:
- Keyword: register
- Variables have the same scope and lifetime as automatic variables
- Variables have no linkage
- Variables are stored in CPU registers for faster access
Example:
#include <stdio.h>
int main() {
register int i; // Declaring a register variable
for (i = 0; i < 10; i++) {
printf("Register variable i: %d\\n", i);
}
return 0;
}
Note that the use of register is only a hint to the compiler, and it may choose to ignore it if there are not enough registers available.