C Storage Classes
In this page:
auto data_type variable_name; // default for locals
register data_type variable_name; // hint: keep in a CPU register
static data_type variable_name; // keeps its value between calls
extern data_type variable_name; // defined elsewhere
Auto Storage Class
auto is the default storage class for any local variable you declare without specifying one explicitly -- it allocates temporary memory for the current function call and is destroyed automatically once that call ends.
Example: Auto Storage Class
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
auto int x = 5;
// Print formatted text to the screen
printf("%d", x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Register Storage Class
register asks the compiler to store a variable directly in a CPU register instead of regular memory (RAM), aiming for faster access -- though modern compilers usually make this optimization decision on their own regardless of the hint.
Example: Register Storage Class
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
register int i;
// Loop: repeat while the condition holds
for (i = 0; i < 3; i++) {
// Print formatted text to the screen
printf("%d ", i);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Static Storage Class
static changes a local variable's lifetime so it retains its value between separate calls to the same function, instead of resetting each time -- useful for things like a counter that needs to persist across multiple invocations.
Example: Static Storage Class
// Include standard input/output (printf, scanf)
#include <stdio.h>
void counter() {
static int count = 0;
count++;
// Print formatted text to the screen
printf("%d ", count);
}
// Program execution starts in main()
int main() {
counter();
counter();
counter();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Extern Storage Class
extern declares that a variable is defined in a different source file, letting multiple .c files share and modify the same global variable without each one needing its own separate copy.
Example: Extern Storage Class
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Declare sharedValue and set it to 42
int sharedValue = 42;
extern int sharedValue;
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d", sharedValue);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Selecting Storage Classes
Choosing the right storage class is about matching intent to behavior: static for a value that must persist across calls, auto for ordinary short-lived locals, and register only as a rarely-needed performance hint on tight loop counters.
Example: Selecting Storage Classes
// Include standard input/output (printf, scanf)
#include <stdio.h>
void counter() {
static int persistent = 0;
// Declare temporary and set it to 0
int temporary = 0;
persistent++;
temporary++;
// Print formatted text to the screen
printf("%d %d ", persistent, temporary);
}
// Program execution starts in main()
int main() {
counter();
counter();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
registerand then taking the variable's address with&, which is not allowed. - Expecting an
autovariable to keep its value between calls. - Using
externwithout a definition in any file, causing a linker error.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: