Storage classes in C define the scope, visibility, lifetime, and default initial value of variables. There are four storage classes in C:
- auto
- extern
- static
- register
1. auto Storage Class
- Definition:
auto
is the default storage class for local variables. - Scope: Local to the block where it is defined.
- Lifetime: Exists until the block/function ends.
- Visibility: Not accessible outside the block.
- Default Value: Garbage (undefined).
Example of Storage classes :
#include <stdio.h>
void testFunction() {
auto int x = 10; // 'auto' is optional, same as int x = 10;
printf("Value of x: %d\n", x);
}
int main() {
testFunction();
return 0;
}
Key Points of Storage classes :
- Automatically allocated when the function/block is called.
- Destroyed when the function/block exits.
- Cannot be accessed outside the function/block.
Interview Questions based on Storage classes:
- What is the default storage class of a local variable in C?
- Can an
auto
variable be accessed outside its function? - How does an
auto
variable differ from astatic
variable?
2. extern Storage Class
- Definition:
extern
is used to declare a global variable in one file and define it in another. - Scope: Global (visible throughout the program).
- Lifetime: Exists as long as the program runs.
- Visibility: Accessible in all files (if declared with
extern
). - Default Value: Zero (0).
Example (Multiple Files Usage) for Storage classes :
File: main.c
#include <stdio.h>
// Declaration of external variable (defined in another file)
extern int count;
void display() {
printf("Count: %d\n", count);
}
int main() {
display();
return 0;
}
File: data.c
// Definition of external variable
int count = 10;
Compile & Run
gcc main.c data.c -o output
./output
Output: Count: 10
Key Points of Storage classes :
- Used for global variables and function definitions across multiple files.
- Declaration using
extern
does not allocate memory, only references it. - Definition (without
extern
) allocates memory.
Interview Questions of Storage classes:
- What is the difference between declaration and definition of a variable?
- Can
extern
be used for functions? (Yes, by default functions areextern
) - What happens if an
extern
variable is not defined anywhere?
3. static Storage Class
- Definition:
static
variables retain their values across function calls. - Scope:
- Local Static Variable: Limited to the function/block where it is defined.
- Global Static Variable: Limited to the file where it is defined.
- Lifetime: Exists throughout the program execution.
- Visibility:
- Local Static: Visible only in the function.
- Global Static: Not accessible outside the file.
- Default Value: Zero (0).
Example 1: Local Static Variable
#include <stdio.h>
void counter() {
static int count = 0; // Retains value between function calls
count++;
printf("Count: %d\n", count);
}
int main() {
counter(); // Output: Count: 1
counter(); // Output: Count: 2
counter(); // Output: Count: 3
return 0;
}
Example 2: Global Static Variable
File: static_var.c
#include <stdio.h>
static int globalCount = 10; // This variable is NOT accessible in other files
void display() {
printf("Global Count: %d\n", globalCount);
}
File: main.c
#include <stdio.h>
// extern int globalCount; // This will cause a linker error!
int main() {
// printf("Global Count: %d\n", globalCount); // Not accessible
return 0;
}
Key Observation: globalCount
is limited to static_var.c
and cannot be accessed in main.c.
Key Points of Storage classes:
- Local Static Variables retain values between function calls.
- Global Static Variables restrict visibility to the file.
- Useful in scenarios like counters, cache implementations, and module-specific global variables.
Interview Questions of Storage classes:
- What is the difference between
static
andauto
variables? - Can we use a
static
variable inside a function? - What happens if we declare a global variable as
static
?
4. register Storage Class
- Definition: Suggests that the variable be stored in CPU registers instead of RAM.
- Scope: Local to the block where it is defined.
- Lifetime: Exists until the function/block ends.
- Visibility: Not accessible outside the function/block.
- Default Value: Garbage (undefined).
- Key Restriction: Cannot use
&
(address-of) operator onregister
variables.
Example:
#include <stdio.h>
void testFunction() {
register int x = 5; // Hints compiler to store in CPU register
printf("Value of x: %d\n", x);
}
int main() {
testFunction();
return 0;
}
Example: Error Case (&
Operator)
#include <stdio.h>
int main() {
register int x = 10;
printf("%p\n", &x); // ❌ Error: Cannot get address of register variable
return 0;
}
Error: cannot take the address of register variable x
.
Key Points of Storage classes:
register
is just a hint; compiler may ignore it.- Useful for performance-critical variables like loop counters.
- No guarantee that the variable will be stored in registers.
Interview Questions of Storage classes:
- What is the purpose of the
register
storage class? - Can we declare a pointer to a
register
variable? (No) - What happens if the compiler cannot store a
register
variable in a CPU register?
Comparison Table of Storage classes :
Storage Class | Scope | Lifetime | Default Value | Special Features |
---|---|---|---|---|
auto | Local | Function/block | Garbage | Default storage class for local variables |
extern | Global | Whole program | Zero (0) | Used for global variables across multiple files |
static | Local/Global | Whole program | Zero (0) | Retains value between function calls, file scope for global static variables |
register | Local | Function/block | Garbage | Suggests CPU register usage, address (& ) not allowed |
FAQ (Frequently Asked Questions) on Storage Classes in C and C++:
1. What are storage classes in C/C++?
Storage classes define the scope, lifetime, and visibility of variables and functions in a program. They determine where and how a variable is stored in memory.
2. What are the different types of storage classes in C/C++?
There are four main storage classes:
- auto – Default for local variables (stored in stack).
- register – Hints to store the variable in a CPU register for faster access.
- static – Retains the value of a variable between function calls.
- extern – Refers to a global variable defined in another file.
3. What is the default storage class for variables in C?
By default, local variables have the auto storage class.
4. What is the difference between auto
and register
storage classes?
Feature | auto | register |
---|---|---|
Storage | Stack | CPU register (if available) |
Scope | Local | Local |
Lifetime | Function call | Function call |
Access via & | Yes | No (register variables don’t have a memory address) |
Speed | Normal | Faster (if stored in a register) |
5. Can we take the address of a register
variable?
No. The register
storage class suggests the variable be stored in a CPU register, which doesn’t have a memory address.
register int x = 10;
printf("%p", &x); // ❌ Error: Cannot take address of register variable
6. What is the purpose of static
storage class in C/C++?
The static
storage class is used to:
- Retain a variable’s value across function calls (inside a function).
- Restrict the scope of a global variable to the same file (internal linkage).
✅ Example (Inside a function):
void counter() {
static int count = 0; // Value persists
count++;
printf("%d\n", count);
}
int main() {
counter(); // Output: 1
counter(); // Output: 2
return 0;
}
✅ Example (File Scope – Internal Linkage):
static int var = 10; // Only accessible within this file
7. What is the difference between static
and extern
variables?
Feature | static | extern |
---|---|---|
Scope | Limited to the same file | Accessible across multiple files |
Lifetime | Throughout the program | Throughout the program |
Use Case | Keeps variable private to the file | Shares variable across files |
8. Can we use static
inside a class in C++?
Yes. In C++, static
can be used inside a class to create class variables (shared across all objects).
✅ Example (C++ static
member variable):
class Test {
static int count; // Shared by all objects
public:
void increment() { count++; }
void show() { std::cout << count << std::endl; }
};
int Test::count = 0; // Must be defined outside the class
9. What happens if we declare a variable as extern
but don’t define it?
If a variable is declared with extern
but not defined anywhere, the compiler will throw a linking error.
✅ Example (Correct Usage across files):
File 1 (file1.c
):
int num = 10; // Global definition
File 2 (file2.c
):
extern int num; // Declaration
printf("%d", num); // ✅ Works fine
❌ Incorrect (Missing Definition):
extern int num;
printf("%d", num); // ❌ Linker Error: Undefined reference to 'num'
10. Can a function be declared as static
?
Yes. A static
function has file scope, meaning it can only be accessed within the file where it is declared.
✅ Example:
static void helper() {
printf("This function is only accessible in this file.");
}
11. What is the difference between static
and global
variables?
Feature | static (Global Scope) | Global Variable |
---|---|---|
Scope | Limited to the file | Accessible in all files |
Lifetime | Program lifetime | Program lifetime |
Use Case | Hides from other files | Accessible everywhere |
✅ Example:
static int a = 10; // Accessible only in this file
int b = 20; // Accessible in all files if declared with `extern`
12. Can a global variable be static
and extern
at the same time?
No. static
restricts visibility to the file, while extern
is used to access a variable globally across multiple files. They conflict with each other.
13. Where are storage class variables stored in memory?
Storage Class | Stored In |
---|---|
auto | Stack |
register | CPU Register (if available) |
static | Data Segment (BSS or initialized) |
extern | Data Segment |
14. What is the difference between static
and const
in C/C++?
Feature | static | const |
---|---|---|
Scope | Can be global or local | Local by default |
Lifetime | Throughout the program | Depends on where it is defined |
Modifiable? | Yes | No (Read-only) |
✅ Example (Difference):
static int x = 10; // Accessible only in this file
const int y = 20; // Cannot be modified
15. Can static
and volatile
be used together?
Yes. static
controls lifetime, while volatile
tells the compiler not to optimize the variable.
✅ Example (Using static volatile
):
static volatile int flag = 1;
🔹 Useful in embedded systems where the variable is modified by hardware (e.g., interrupts).
16. Can we use extern
with functions?
Yes. By default, functions in C have external linkage, so extern
is redundant but still valid.
✅ Example:
extern void func(); // Declaration
void func() {
printf("Hello World");
}
17. How does extern "C"
work in C++?
C++ uses name mangling, so extern "C"
ensures the function is linked using C-style linkage (useful when calling C functions from C++).
✅ Example (C++ Code Calling C Function):
extern "C" void hello(); // Declaring C function
18. Which storage class should be used for embedded programming?
register
→ Fast access variables.static
→ Retain values (e.g., sensor data).volatile
→ Prevent compiler optimizations (e.g., ISR flags).extern
→ Share global variables across files.
19. Why is static
used in embedded systems?
- Reduces RAM usage by keeping variables in non-volatile memory (data segment).
- Avoids unnecessary stack allocation.
- Improves performance by retaining values across function calls.
20. What are the best practices for using storage classes?
✔️ Use static
for file-private variables.
✔️ Use register
for frequently accessed variables (if needed).
✔️ Use extern
for global variables shared across files.
✔️ Avoid too many global variables (use static
instead).
Here are 20 Multiple Choice Questions (MCQs) on Storage Classes in C and C++:
1. What is the default storage class for local variables in C?
a) auto
b) static
c) register
d) extern
✅ Answer: a) auto
2. Which storage class is used to retain a variable’s value across function calls?
a) auto
b) static
c) register
d) extern
✅ Answer: b) static
3. Where are register
variables stored?
a) Heap
b) Stack
c) CPU Register
d) Data Segment
✅ Answer: c) CPU Register
4. Which storage class makes a global variable accessible across multiple files?
a) static
b) extern
c) register
d) auto
✅ Answer: b) extern
5. What happens if you take the address of a register
variable?
a) It returns the memory address
b) It results in a compilation error
c) It returns NULL
d) It gives unpredictable behavior
✅ Answer: b) It results in a compilation error
6. What is the lifetime of a static
variable declared inside a function?
a) Until the function exits
b) Throughout the program execution
c) Until the next function call
d) Depends on compiler optimization
✅ Answer: b) Throughout the program execution
7. Which storage class ensures a function is not accessible from other files?
a) extern
b) register
c) static
d) auto
✅ Answer: c) static
8. Where are static
variables stored in memory?
a) Stack
b) Heap
c) Data Segment
d) CPU Register
✅ Answer: c) Data Segment
9. What is the scope of an auto
variable?
a) Function-level (local scope)
b) Global scope
c) File-level
d) System-wide
✅ Answer: a) Function-level (local scope)
10. What is the primary purpose of the extern
keyword?
a) To make variables private to a file
b) To declare a variable defined in another file
c) To store a variable in a CPU register
d) To keep a variable’s value persistent
✅ Answer: b) To declare a variable defined in another file
11. Which keyword is used to restrict a global variable’s access to the same file?
a) extern
b) auto
c) static
d) volatile
✅ Answer: c) static
12. Which of the following is true about register
variables?
a) They are guaranteed to be stored in CPU registers
b) They can be accessed from other files
c) They have a local scope
d) They can be modified by external programs
✅ Answer: c) They have a local scope
13. How many times is memory allocated for a static
variable in a function?
a) Every time the function is called
b) Only once
c) Never
d) Once per function call
✅ Answer: b) Only once
14. Can a static
variable be initialized inside a function?
a) No, it must be global
b) Yes, but it retains its value across calls
c) No, only extern
variables can
d) Yes, but it is destroyed after function exits
✅ Answer: b) Yes, but it retains its value across calls
15. What will happen if a global variable is declared as static
?
a) It can be accessed from other files
b) It will be stored in CPU registers
c) It will be limited to the current file
d) It will cause an error
✅ Answer: c) It will be limited to the current file
16. Which storage class should be used for sharing global variables across multiple files?
a) auto
b) static
c) register
d) extern
✅ Answer: d) extern
17. What is the difference between static
and extern
storage classes?
a) static
restricts variable scope, extern
extends it
b) static
is for global variables only
c) extern
variables cannot be modified
d) static
variables cannot be initialized
✅ Answer: a) static
restricts variable scope, extern
extends it
18. In C++, what does static
do when used inside a class?
a) Creates a variable that belongs to a single instance
b) Makes the variable global
c) Creates a class variable shared by all objects
d) Prevents inheritance
✅ Answer: c) Creates a class variable shared by all objects
19. Where are extern
variables stored?
a) Heap
b) Stack
c) Data Segment
d) Register
✅ Answer: c) Data Segment
20. Which storage class should be used for variables that must be frequently accessed?
a) static
b) register
c) extern
d) auto
✅ Answer: b) register
Thank you for exploring this Storage classes tutorial ! Stay ahead in embedded systems with expert insights, hands-on projects, and in-depth guides. Follow Embedded Prep for the latest trends, best practices, and step-by-step tutorials to enhance your expertise. Keep learning, keep innovating!