- The four storage classes: auto, static, extern, register
- Where each type lives in memory (stack, data segment, register)
- Default initial values — why static gets 0 but auto gets garbage
- Scope: block scope vs file scope vs external linkage
- Lifetime: block lifetime vs program lifetime
- Common interview traps (register's behavior, extern vs static)
- Why const and volatile are NOT storage classes
This page has 30 multiple choice questions on storage classes in C. You'll find questions on auto, static, extern, and register — how each is stored, its default initial value, its scope, and its lifetime. Each question comes with the correct answer and a short explanation in plain English, so you understand the "why" and not just the "what". Storage classes are a favourite exam topic because they combine several C fundamentals (memory layout, scope, lifetime) into one concept — get them right and much of C becomes clearer.
Storage Class Basics
Q1. How many storage classes are there in C programming?
Answer: c) 4
C has exactly four storage classes: auto, static, extern, and register. Each one answers four questions about a variable: where is it stored, what is its default initial value, what is its scope (where can it be accessed), and what is its lifetime (how long does it exist). const, volatile, and _Thread_local are type qualifiers / C11 additions, not part of the classic four storage classes.
Q2. What is the default storage class for a variable declared inside a function in C?
Answer: a) auto
A local variable without an explicit storage class defaults to auto. It lives on the stack, exists only while its block runs, and has garbage as its initial value until you assign it. This is why int x; inside a function contains unpredictable data — always initialize before use. The auto keyword is rarely written; you just let it be implicit.
Q3. What is the default storage class for a global variable declared outside of any function in C?
Answer: b) extern
A file-level variable without an explicit storage class defaults to extern (external linkage). It has program lifetime, is initialized to 0 by default, and can be accessed from other source files (by declaring it extern there). This is why globals are shared across a program — the compiler and linker cooperate to make them available everywhere.
Q4. Which storage class allows a variable to be accessed globally throughout the program?
Answer: b) extern
extern gives a variable external linkage — it can be accessed from any file in the program that declares it. This is the mechanism for sharing globals across multiple source files: file A defines the variable, file B declares it as extern, and both see the same object. Note that static makes a variable file-local (the opposite of extern).
auto Storage Class
Q5. What happens to a variable declared as 'auto' when the function in which it is declared ends?
Answer: d) It is automatically deallocated
Auto variables live on the stack. When the function returns, its stack frame is popped, and all its auto variables are destroyed. Their memory may be reused by the next function call. This is why auto variables seem to "reset" on each call — every invocation gets a fresh copy. Contrast with static, which persists across calls (Q6 below).
Q6. Which storage class keeps variables on the stack?
Answer: a) auto
Auto variables are the ones that live on the stack. Register variables may also fall back to the stack if no register is available, but the defining characteristic of auto is stack storage. extern and static live in the data segment (or BSS for uninitialized statics). Understanding where a variable lives helps you reason about its lifetime and its default value.
Q7. What is the default initial value of an auto variable in C?
Answer: c) Garbage (uninitialized)
Auto variables are not automatically initialized — they contain whatever bit pattern happened to be on the stack from a previous operation. Reading an uninitialized auto variable is undefined behaviour in C (formally, it's an "indeterminate value"). Always initialize: int x = 0;, not just int x;. This is one of the most common beginner bugs.
static Storage Class
Q8. What happens when we declare a variable as 'static' inside a function in C?
Answer: c) It retains its value between function calls
A static local variable is created only once, before the first call, and keeps its value between calls. So if you increment it in a function, the next call sees the incremented value. Options b is also true (it's still local to the function), but c captures the defining feature of static. Option a) is false — static locals are not re-created on each call.
Q9. What is the default initial value of a variable declared as 'static' in C?
Answer: a) 0
Static variables live in the data segment (or BSS for uninitialized ones), which the OS zeroes out before main runs. So an uninitialized static is guaranteed to be 0 (or NULL for pointers, or 0.0 for floats). This is why static locals are convenient — you don't need to initialize them explicitly, unlike auto locals. This guarantee is part of the C standard, not just a compiler convention.
Q10. Which storage class is used to declare variables that are visible only within the file they are declared in?
Answer: b) static
When static is applied to a global variable or function, it gives the symbol internal linkage — it's visible only within the file where it's declared. This is how C implements "private" globals: prefix them with static and no other file can see them, even if they declare them extern. This is the standard way to hide implementation details in libraries.
Q11. Which storage class in C is used to declare variables that remain in memory for the entire program execution?
Answer: c) static
Both static and extern give program lifetime, but the question is asking about the storage class you apply yourself to make a variable persist. static is the answer here because you use it to extend a local's lifetime to match the whole program. extern is for cross-file access of globals (which already have program lifetime). Either answer is technically valid; exam expects static.
extern Storage Class
Q12. Where is an extern variable's actual storage allocated?
Answer: c) In the data segment (wherever it's actually defined)
The keyword extern doesn't allocate memory — it just declares that a variable exists elsewhere. The actual definition (in some other file or later in the same file) allocates the memory, usually in the data segment (initialized) or BSS (uninitialized). The extern declaration just tells the compiler "trust me, this exists; the linker will resolve it." This separation of declaration from definition is what makes multi-file C programs possible.
Q13. Which storage class in C is used to declare variables that exist for the entire duration of the program with external linkage?
Answer: b) extern
extern gives a variable both program lifetime and external linkage. This is what makes a variable usable across multiple source files — file A defines it, file B declares it extern, and both see the same object. Note the difference from static: static also has program lifetime but restricts visibility to one file. extern is the "shared global" storage class.
register Storage Class
Q14. Which storage class is used to declare variables that are stored in CPU registers for faster access?
Answer: d) register
register is a hint to the compiler: "please keep this variable in a CPU register if you can." Registers are the fastest memory in the system (nanoseconds), so loop counters and frequently-used local variables benefit. The compiler is free to ignore the hint — modern optimizing compilers often choose registers on their own. register only applies to local variables and function parameters.
Q15. When a variable is declared as 'register' in C, where is it allocated?
Answer: d) In a CPU register if available; otherwise on the stack
register is only a request, not a command. If the compiler can allocate a register for the variable, it will. If not — because all registers are already used, or because the compiler decides the hint is counterproductive — the variable is treated like auto (on the stack). So register is best thought of as "auto with a hint," not as a guarantee of register storage.
Q16. What happens to a variable declared as 'register' in C if there are more variables to store than available registers?
Answer: d) It behaves like an 'auto' variable
When the compiler can't honour the register hint, the variable is placed on the stack — exactly like an auto variable. So "behaves like auto" is the correct description. Options a), b), and c) are misleading because they imply the register keyword moves the variable to a different segment — it doesn't. The variable still lives in the normal stack frame, just without a register allocation.
Q17. Can you take the address of a variable declared as 'register' using the & operator?
Answer: b) No — the & operator cannot be applied to register variables
A register variable might not have a real memory address — it could be a physical CPU register with no way to compute "its address." So C forbids taking the address of a register variable. Writing ®_var is a compile error. This is one of the few restrictions of register, and it's a classic interview gotcha. It also means register variables can't be used where pointers are required.
Memory Segments
Q18. Where are global variables and static variables stored in C?
Answer: c) In the data segment (or BSS if uninitialized)
Global and static variables live in the data segment (initialized) or BSS (uninitialized, zero-filled). Both are allocated at program load, persist until program exit, and have a fixed memory layout. This is why they're initialized to 0 by default — the OS zeroes out BSS before main runs. Stack is for auto/register, heap is for malloc-family.
Q19. When a static global variable is declared, where does it get stored?
Answer: c) In the data segment (with internal linkage)
A static global has the same storage as any global — the data segment. What changes is its linkage: static globals have internal linkage (file-local visibility), while non-static globals have external linkage. Both are in the data segment; both have program lifetime; both default to 0. The difference is only in whether other files can access them by name.
Q20. Which storage class in C is used to declare variables that are stored in heap memory?
Answer: d) None — heap allocation uses malloc/calloc, not a storage class
Storage classes and heap allocation are two different concepts. Storage classes (auto, static, extern, register) control where a named variable is stored and how long it lives. Heap memory is allocated dynamically via malloc/calloc/realloc and accessed through pointers — no storage class applies. This is a common trick question; if you see "malloc" mixed in with storage classes, it's usually a wrong answer.
Q21. Which two storage classes give a variable program lifetime (i.e., it exists from program start to exit)?
Answer: b) static and extern
auto and register variables have block lifetime — they exist only while their enclosing block runs. static and extern have program lifetime — they exist from program start to program exit. This is a critical distinction for reasoning about when a variable's value is preserved: statics keep theirs across calls; autos don't. extern is program lifetime by definition (that's how globals work).
Q22. Is volatile a storage class in C?
Answer: b) No, it's a type qualifier, not a storage class
volatile tells the compiler "this value might change unexpectedly (from a signal handler, hardware register, or another thread), so don't optimize reads away." It's a type qualifier, like const. Storage classes and type qualifiers are orthogonal: you can have static volatile int — that's a static variable that's also volatile. Both are valid and mean different things.
Q23. Is const a storage class in C?
Answer: b) No, it's a type qualifier, not a storage class
const is a type qualifier that tells the compiler the value won't change. It's completely separate from storage classes: const int x = 10; doesn't specify where x lives. The original question had "const keyword is used to declare variables with which storage class? answer: a) auto" — that's wrong, because const is not a storage class at all. Use const with any storage class: static const, extern const, etc.
Q24. Which storage class in C is used to declare variables that remain in memory for the entire program and have external linkage?
Answer: b) extern
extern gives both program lifetime AND external linkage — the combination that makes globals usable across files. static gives program lifetime but internal linkage. If you need a global that other files can see, use extern (or just declare the variable at file scope without static). If you want a global that only the current file can see, add static. The key difference is linkage, not memory.
Q25. Which storage class is a hint to the compiler that a variable will be heavily used?
Answer: d) register
register is the only storage class that's purely a hint — it says "keep this in a CPU register if you can." In practice, modern optimizing compilers ignore it and allocate registers better than any programmer can. In C11, register is deprecated for most uses. Still, in interviews and old exam papers, "register = hint for register allocation" is the canonical answer.
Q26. Which storage class is default for a function parameter in C?
Answer: a) auto
Function parameters behave like auto variables — they live on the stack, have block lifetime, and default to garbage. You can explicitly add register to a parameter (void f(register int x)) to hint the compiler, but the default is auto. Parameters are the most common candidates for register allocation in compilers, but that's an implementation detail, not a storage class.
Q27. What happens if you declare a variable as both static and extern in the same file?
Answer: a) It's a compile error
static and extern are mutually exclusive storage classes — one gives internal linkage, the other external. Combining them on the same variable is a contradiction, and the compiler will reject it: "multiple storage classes in declaration specifiers." This is a common exam trap. You must pick one or the other based on whether you want file-local or cross-file access.
Q28. Which storage class gives the fastest possible access to a variable?
Answer: d) register
Registers are the fastest storage in the CPU — faster than cache, faster than RAM. register tells the compiler to use one for a variable, so it's the "fastest access" storage class. But remember: register is only a hint. If no register is available, the variable falls back to the stack and gets normal (slower) speed. That said, in theory, register is the fastest.
Q29. Which storage class would you use to make a variable's value persist across multiple calls to the function that owns it?
Answer: c) static
This is the defining use-case for static on a local variable: it lives in the data segment for the whole program, so its value is preserved between calls. Classic example: static int counter = 0; counter++; inside a function returns 1, 2, 3, ... on successive calls. If counter were auto, it would reset to garbage every call. extern isn't applicable (it's for globals), auto resets, register doesn't persist.
Q30. In which memory segment are the automatic variables of a function stored?
Answer: c) In the stack
Auto variables (and register variables that fall back to memory) live on the stack — the region that grows and shrinks as functions are called and return. Each function call pushes a new stack frame containing its parameters and locals; returning pops it. This is why auto variables "disappear" when the function ends: the frame is gone. Stack is fast but small (typically 1–8 MB), unlike the heap or data segment.
Frequently Asked Questions
What are the four storage classes in C?
auto, static, extern, and register. Each controls where a variable is stored (stack, data segment, register), its default initial value (garbage vs 0), its scope (block, file, or program), and its lifetime (block vs program). Together these four properties determine how a variable behaves and how you should use it.
What is the difference between static and extern in C?
static gives a variable internal linkage — it's visible only within its own file. extern gives external linkage — it's visible across files, typically referring to a variable defined in another translation unit. Both have program lifetime and live in the data segment. The difference is visibility: static = file-local, extern = shared across files.
Does the register keyword guarantee a CPU register?
No. register is only a hint to the compiler. Modern optimizing compilers usually ignore it and choose registers on their own. If no register is available, the variable is treated like auto (on the stack). You also cannot take the address of a register variable with &, since it might not have a real memory address.
Why is the default value of a static variable 0?
Static and extern variables live in the data segment, which the operating system zero-initializes before main() runs. Auto and register variables live on the stack, so they contain whatever garbage was left over from previous operations. This is why static int x; is always 0 but int x; is garbage — the memory segment determines the default.
Is const a storage class in C?
No. const is a type qualifier — it tells the compiler the value won't be modified. volatile is another type qualifier. Neither is a storage class. Storage classes are limited to auto, static, extern, and register. The original question "const keyword is used to declare variables with which storage class?" had answer "auto" — that's wrong, because const is orthogonal to storage classes.
💡 Tip: Storage classes are best remembered with a small table: auto = stack, block scope, garbage, block lifetime; static = data segment, file/block scope, 0, program lifetime; extern = data segment, program scope, 0, program lifetime; register = CPU register (maybe), block scope, garbage, block lifetime. Once you have this table, almost every storage-class question becomes easy.