The storage classes in C determine the lifetime, visibility, memory location, and initial value of variables. There are four storage classes in C:
-
Automatic
-
External
-
Static
-
Register
| Storage Class | Memory Location | Default Value | Scope | Lifetime |
|---|---|---|---|---|
| auto | RAM | Uninitialized Value | Local | Inside Function |
| extern | RAM | 0 | Global | Declared anywhere in the program before the end of the main program |
| static | RAM | 0 | Local | Retains value between multiple function calls until the end of the main program |
| register | Register | Uninitialized Value | Local | Inside Function |
Automatic
-
Automatic variables are allocated memory automatically at runtime.
-
The visibility of automatic variables is limited to the block where they are defined.
-
The scope of automatic variables is limited to the block where they are defined.
-
Automatic variables are initialized to garbage values by default.
-
The memory allocated to automatic variables is released upon exiting the block.
-
The keyword for defining automatic variables is auto.
-
In C, every local variable is an automatic variable by default.
Example 1
#include <stdio.h>
int main() { int a; // Automatic variable char b; float c; printf("%d %c %f", a, b, c); // Print initial default values of automatic variables a, b, and c return 0;}
Output: garbage garbage garbage
Example 2
#include <stdio.h>
int main() { int a = 10, i; printf("%d ", ++a); { int a = 20; for (i = 0; i < 3; i++) { printf("%d ", a); // Since a is local value, it will print 20 three times } } printf("%d ", a); // Since the scope of a = 20 has ended, it will print 11 return 0;}
Output: 11 20 20 20 11
👇 Click to claim 👇
👉 C Language Knowledge Resource Collection
Static
-
Variables defined with the static keyword can retain their values between multiple function calls.
-
Static local variables are only visible to the function or block that defines them.
-
The same static variable can be declared multiple times, but can only be initialized once.
-
The default initial value of static integer variables is 0, otherwise null.
-
The visibility of static global variables is limited to the file declaring them.
-
The keyword for defining static variables is static.
Example 1
#include <stdio.h>
static char c;static int i;static float f;static char s[100];
void main() { printf("%d %d %f %s", c, i, f, s); // Print initial default values of variables c, i, and f}
Output: 0 0 0.000000 (null)
Example 2
#include <stdio.h>
void sum() { static int a = 10; static int b = 24; printf("%d %d \n", a, b); a++; b++;}
void main() { int i; for (i = 0; i < 3; i++) { sum(); // Static variables retain their values between multiple function calls }}
Output: 10 2411 2512 26
Register
-
Variables defined with the register keyword are allocated based on the remaining memory in the CPU registers.
-
It is not possible to take the address of register variables, i.e., the & operator cannot be used to obtain the address of register variables.
-
Access time for register variables is faster than that for automatic variables.
-
The default initial value of register local variables is 0.
-
Variables defined with the register keyword should ideally be stored in CPU registers, but whether they are stored in registers depends on the compiler.
-
Pointers can be stored in registers, meaning registers can hold the addresses of variables.
-
Static variables cannot be stored in registers, as the same variable cannot use multiple storage class specifiers.
Example 1
#include <stdio.h>
int main() { register int a; // Variable a allocated in CPU register, initial default value is 0 printf("%d", a);}
Output: 0
Example 2
#include <stdio.h>
int main() { register int a = 0; printf("%u", &a); // This will cause a compile-time error as address of register variable a cannot be obtained}
Output: main.c:5:5: error: address of register variable ‘a’ requestedprintf(“%u”, &a);^~~~~~
External
-
The external storage class is used to inform the compiler that an extern variable is declared elsewhere in the program.
-
Variables declared as extern do not allocate any memory; they are merely declarations used to specify variables declared in other parts of the program.
-
The default initial value of external integer variables is 0, otherwise null.
-
Extern variables can only be initialized at the global scope, i.e., cannot be initialized within any block or method.
-
External variables can be declared multiple times but can only be initialized once.
-
If a variable is declared as external, the compiler will search for its initialization location in the program, which can be extern or static. If not found, the compiler will show an error.
Example 1
#include <stdio.h>
int main() { extern int a; printf("%d", a);}
Output: main.c:(.text+0x6): undefined reference to `a’collect2: error: ld returned 1 exit status
Example 2
#include <stdio.h>
int a;
int main() { extern int a; // Variable a defined globally, no memory allocated for a printf("%d", a);}
Output: 0
Example 3
#include <stdio.h>
int a;
int main() { extern int a = 0; // This will show a compile-time error as extern cannot be used with an initializer printf("%d", a);}
Output: Compile-time errormain.c: In function ‘main’:main.c:5:16: error: ‘a’ has both ‘extern’ and initializerextern int a = 0;^~~~~~
Example 4
#include <stdio.h>
extern int a;
int a = 20;
int main() { printf("%d", a);}
Output: 20
Example 5
extern int a;int a = 10;#include <stdio.h>
int main() { printf("%d", a);}
int a = 20; // This will show a compile-time error
Output: Compile-time error
Programmer Technical Exchange Group
Scan the QR code to join the group, remember to note: city, nickname, and technical direction.