← Back to C Course | Chapter 5: Functions | Lesson 7 of 8

C Storage Classes

Storage classes decide how long a variable lives and where it can be seen, like different kinds of lockers with different rules. auto, static, register and extern are the choices.
Syntax
c
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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using register and then taking the variable's address with &, which is not allowed.
  2. Expecting an auto variable to keep its value between calls.
  3. Using extern without 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.