Storage Classes in C MCQs: 12 Solved Questions with Explanations

Test your storage-class basics with 12 fully explained C questions, then learn the code-reading method for static recursion, pointer lifetime and extern declarations.

KnowledgeGate Team

Exam prep & CS education

Updated 18 Aug 20267 min read

Storage classes look like four small C keywords: auto, register, static and extern. Yet exam questions turn them into traps involving scope, output, recursion and declarations. Memorising one table is not enough if you cannot trace what the code does.

The traps repeat in a small number of shapes: a static local sitting inside a recursive function, a pointer that stays valid only because it points at a static object, a runtime value used where a constant initializer is required, and an extern declaration mistaken for a definition.

The four storage classes in one look

Every storage class helps answer four questions: where the object is normally stored, what its default value is, where its name is visible, and how long the object survives.

Keyword

Storage location

Default value

Scope

Lifetime

auto

Stack

Garbage

Block

Block

register

CPU register

Garbage

Block

Block

static

Data segment

0

Block or File

Whole program

extern

Data segment

0

Global (external linkage)

Whole program

This is exam shorthand. More precisely, an uninitialised automatic object has an indeterminate value, and register is a compiler hint rather than a guarantee. An extern declaration refers to an object defined elsewhere and does not allocate it.

The C language standard defines the storage-class specifiers and their rules. It also requires objects with static storage duration to be zero-initialised when no explicit initializer is supplied. This is why static and defined global objects begin at zero, while ordinary uninitialised local variables do not.

Reference table of C storage classes auto, register, static and extern with their storage location, default value, scope and lifetime.

Keyword-meaning MCQs

Q1. What does the auto storage class mean?

A) Automatic storage

B) Automatic initialisation

C) Automatic typing

D) None of the above

Answer: A) Automatic storage. The object is created when execution enters its block and its lifetime ends when that block exits. auto is the default for local variables, so programmers rarely write it. It does not provide type inference in C.

Q2. Which keyword is used for a storage class?

A) external

B) scanf

C) auto

D) printf

Answer: C) auto. The four storage-class keywords relevant here are auto, register, static and extern. external is not the keyword, while scanf and printf are library functions.

Q3. Which storage class provides the shortest access time?

A) auto

B) register

C) extern

D) static

Answer: B) register. It requests storage in a CPU register, where access can be faster than main memory. A modern compiler may ignore the request and allocate the variable normally, so register expresses intent rather than a guarantee. It is still the only one of the four keywords that asks for register storage at all.

Scope and visibility MCQs

Q4. Which storage class has global visibility in C or C++?

A) auto

B) extern

C) static

D) register

Answer: B) extern. An external declaration can refer to the same externally linked object from another translation unit. auto and register are block-scoped. At file scope, static gives internal linkage, which restricts the name to one source file.

Q5. Which specifier limits a variable's scope to its source file?

A) auto

B) extern

C) register

D) static

Answer: D) static. A file-scope static object has internal linkage, so its name is private to that .c file. Keep Q4 and Q5 together: extern connects a name across files, while file-scope static hides it from other files.

Q6. What is the scope of a register variable?

A) File

B) Function

C) Block

D) None of the above

Answer: C) Block. A register variable is a local with block scope and automatic storage duration. It cannot be the operand of the address operator &.

Default value, lifetime and definition MCQs

Q7. What is the default value of a static variable?

A) Garbage value

B) 0

C) -1

D) 1

Answer: B) 0. An object with static storage duration is zero-initialised when no explicit initializer is present. This is different from an uninitialised auto or register local, whose value is indeterminate.

Q8. What is the default initial value of a global static int?

A) Undefined

B) 0

C) 1

D) -1

Answer: B) 0. The same zero-initialisation rule applies at file scope. Global placement changes visibility, not this default value.

Q9. Which specifier cannot define a local variable of its own?

A) register

B) auto

C) extern

D) static

Answer: C) extern. auto, register and static can all define objects inside a function. An extern declaration may appear inside a function, but it refers to an object with linkage that is defined elsewhere. It does not create a separate local object.

Worked example 1: static keeps its value across calls

Q10. What does this program print?

#include <stdio.h>
int f(int n){
    static int r = 0;
    if (n <= 0) return 1;
    if (n > 3){ r = n; return f(n-2)+2; }
    return f(n-1)+r;
}
int main(){ printf("%d", f(5)); }

A) 5

B) 7

C) 9

D) 18

Answer: D) 18. There is one shared r, not a new r for every call.

  1. f(5) sets r = 5, then needs f(3) + 2.

  2. f(3) needs f(2) + 5.

  3. f(2) needs f(1) + 5.

  4. f(1) needs f(0) + 5.

  5. f(0) returns 1.

  6. Unwinding gives f(1) = 1 + 5 = 6, f(2) = 6 + 5 = 11, f(3) = 11 + 5 = 16, and f(5) = 16 + 2 = 18.

The trap is lifetime. r is initialised once, set to 5 in the first call, and remains 5 for every later addition.

Call-stack trace of f(5) with one shared static r fixed at 5, unwinding through f(3), f(2), f(1) and f(0) to a final answer of 18.

Worked example 2: static pointers and initialisers

Q11. What does this program print?

#include <stdio.h>
void fun(int **p){ static int q = 10; *p = &q; }
int main(){
    int r = 20;
    int *p = &r;
    fun(&p);
    printf("%d", *p);
    return 0;
}

A) 5

B) 10

C) 15

D) None of these

Answer: B) 10. Initially p points to r. The call passes the address of p, so *p = &q redirects the caller's pointer to q. Because q is static, it still exists after fun returns. Dereferencing the pointer prints 10. An address to an automatic local would dangle after the function returned.

Q12. What happens when this program is compiled?

#include <stdio.h>
int main(){
    int x = 10;
    static int y = x;
    if(x == y) printf("Equal");
    else if(x > y) printf("Greater");
    else printf("Less");
    return 0;
}

A) It prints Equal

B) It prints Greater

C) It prints Less

D) Compiler error

Answer: D) Compiler error. A block-scope static object needs a constant-expression initializer. x is an automatic runtime variable, even though the code gives it the value 10. Therefore static int y = x; is not a valid C initializer.

A related trap is placing static int q; inside a C structure definition. static is not a valid storage-class specifier for a C structure member, so that declaration also fails to compile.

Declaration versus definition: the extern trap

Q9 turns on that distinction, and this program shows both halves of it:

#include <stdio.h>
int main(){
    extern int a;
    printf("%d\n", a);
    return 0;
}
int a = 20;

Here extern int a; is a declaration. It promises that a is defined somewhere. The later int a = 20; is the definition because it allocates and initialises the object. The program prints 20. Remember one definition, with as many compatible extern declarations as needed.

Common traps to re-read before the exam

  • "Static resets on every call." No. A static local is initialised once and keeps its value, as Q10 shows.

  • "Static begins with garbage." No. Static objects are zero-initialised by default. Uninitialised automatic locals have indeterminate values.

  • "Extern defines the variable." An extern declaration alone does not allocate the object. A definition must exist somewhere.

  • "The address of a register variable is available." C does not allow &register_variable.

  • "Static data members work inside a C structure." They do not. Do not import this C++ idea into C.

  • "A runtime value can initialise a static local." It cannot. Q12 needs a constant expression.

How exams test storage classes

GATE and placement questions rarely stop at asking what a keyword means. They place a static variable inside recursive code, ask whether a pointer remains valid, or make you separate an extern declaration from a definition. If the paper mixes formats, the MCQ, MSQ and NAT question-type guide explains how to read the expected response before solving.

The useful method is simple: mark each object's scope and lifetime before tracing values. For a static local, draw one shared box outside the call stack. For extern, find the single definition before reasoning about the value. The same tracing habit runs through the C Programming Course, where storage classes sit next to pointers and structures rather than in isolation.

The short version

Remember the pairs. auto and register are block-scoped and uninitialised by default. static and defined global objects are zero-initialised and survive for the whole program. A static local keeps its value between calls, while an extern declaration still needs a definition.

About 50 more solved questions on these four keywords are collected in the storage-classes previous-year question set, which is where to go for more output tracing. The C Language Course builds the same scope and lifetime reasoning up from declarations. If Q11 was the one that caught you, C Pointer Basics: 12 Solved MCQs on Dereferencing takes the pointer half further.