Storage Classes in C Interview Questions and Answers: Scope, Lifetime and Linkage Traces

Prepare speakable answers for storage-class interviews. Trace auto, register, static and extern across blocks, recursion and two C source files.

KnowledgeGate Team

Exam prep & CS education

Updated 3 Dec 20266 min read
Storage Classes in C Interview Questions and Answers: Scope, Lifetime and Linkage Traces

Storage-class questions turn on scope, storage duration, linkage, declarations and definitions. In the two-file example, total moves from 10 to 15 to 21; recursive and block-scope static examples expose lifetime mistakes. State the governing property before tracing a value or rejecting code.

1. Storage class means more than one property

Question 1: What does a storage class tell us in C? Storage-class specifiers determine object duration and identifier linkage; declaration location sets scope. They do not just locate variables.

Question 2: How do scope, storage duration and linkage differ? Scope says where a name is usable, duration how long it exists, and linkage whether declarations name one entity. File-scope int total = 10; has file scope from its declaration onward, static duration and external linkage. Function-local static int calls; has block scope, static duration and no linkage.

Question 3: Does C have exactly four storage classes? The classic interview set is auto, register, static and extern. In C17, _Thread_local is also a specifier. typedef creates a type alias, not an object with storage duration. State the version before saying "only four". See Storage Classes in C: Scope, Lifetime & Exam Traps for the wider map. For broader C foundations, read C Language Concepts Interview Questions and Answers; this page concentrates on storage-class specifiers, cross-file linkage and lifetime traces.

Concept map comparing C declarations by scope, duration, linkage, and observed values.

2. auto and register: automatic duration without myths

Question 4: What does auto mean, and is it the default? In C17, ordinary block-scope int base = 5; has automatic duration unless another specifier changes it. auto int base = 5; states that rule explicitly. Each entry creates a fresh object that ends at block exit. Uninitialised automatic int value; is indeterminate, not zero, so reading it before assignment gives no legitimate output.

Question 5: Does register guarantee CPU-register storage? No. It requests fast access, not physical placement. The object still has automatic duration, and C17 forbids applying unary & to it. In int sample(void) { auto int base = 5; register int step = 2; base += step; return base; }, two calls return 7 and 7 because both objects are recreated and initialised each time.

Parameters also have automatic duration. Recursive calls get separate parameters and ordinary locals, while mutable block-scope static is shared.

3. static has two meanings, depending on its location

Question 6: What does static mean inside a function? A block-scope static has block scope, no linkage and program-long duration. In int next(void) { static int n = 10; n += 3; return n; }, three calls return 13, 16 and 19. There is one n; outside code cannot name it. Compare ordinary locals in Functions in C: 12 Solved MCQs on Prototypes & Types.

Question 7: What does static mean at file scope? File-scope objects already have static duration. In static int file_hits;, static gives internal linkage, keeping the name within its translation unit. For static void helper(void), the name has internal linkage; functions have no object storage duration.

Question 8: When is a static object initialised? A static-duration object is zero-initialised if needed and initialised before program start. A block-scope static is not reinitialised per call. Standard C requires a valid static initializer, so static int seed = read_seed(); is not portable C even if similar C++ works.

4. extern, declaration and definition across files

Question 9: What does extern do? It normally lets a scope name an object or function defined elsewhere. It does not copy the object. extern int total; without an initializer is a declaration, and compatible external-linkage declarations can denote one object.

Question 10: Does extern allocate memory? A bare "no" is unsafe. Here, extern int total; without an initializer is not an object definition and creates no second object. int total = 10; defines and initialises the object. File-scope extern int total = 10; is also a definition because it has an initializer. File-scope int total; is a tentative definition, not an uninitialised automatic local.

Question 11: Can another file expose a file-scope static object with extern? No. If counter.c has static int file_hits;, extern int file_hits; in main.c seeks an external-linkage definition, not that internal object. Use one external definition, compatible extern declarations in a shared header or consumer, and file-scope static for translation-unit-private names.

5. Worked two-file storage-class trace

Question 12: What does this program print, and why is there only one total?

counter.c

c
#include <stdio.h>

int total = 10;
static int file_hits;

void add(int x) {
    static int calls;
    int adjusted = x;

    ++calls;
    ++file_hits;
    adjusted += calls;
    total += adjusted;

    printf("%d %d %d %d\n",
           calls, adjusted, file_hits, total);
}

main.c

c
#include <stdio.h>

extern int total;
void add(int x);

int main(void) {
    add(4);
    add(4);
    printf("total=%d\n", total);
    return 0;
}

Stage

calls

file_hits

adjusted

total

Output

Before call 1

0

0

not alive

10

none

Call 1

0 to 1

0 to 1

4 to 5

10 to 15

1 5 1 15

After call 1

1

1

not alive

15

none

Call 2

1 to 2

1 to 2

fresh 4 to 6

15 to 21

2 6 2 21

Back in main

2

2

not alive

21

total=21

Call 1 computes adjusted = 4 + 1 = 5, then total = 10 + 5 = 15. That automatic object ends. Call 2 creates a new adjusted = 4, computes 4 + 2 = 6, then total = 15 + 6 = 21. The extern declaration denotes the same total. Complete output:

Code
1 5 1 15
2 6 2 21
total=21
Two-file C trace showing total, file_hits, calls, and adjusted values across two calls.

6. Storage-class traps that break memorised answers

Question 13: Can I return the address of a local variable? int *bad(void) { int value = 9; return &value; } returns an automatic object's address. It ends on return, so dereferencing later is undefined, with no valid number. static int value = 9; survives but is shared.

Question 14: Does a local static reset in recursion? In int visits(int n) { static int count; ++count; if (n == 0) return count; return visits(n - 1); }, visits(2) increments shared zero-starting count in three frames and returns 3; later visits(1) increments twice and returns 5, not 2. Shared state makes the function non-reentrant; concurrent use needs isolation or synchronisation.

Question 15: Which quick claims should I reject?

Claim

Correction

Local static gives global scope

Scope stays local.

Globals need static for static duration

File scope provides it.

extern creates a copy

Declarations may share one object.

Automatic locals start at zero

Not when uninitialised.

register guarantees hardware placement

Request only.

&register_object is valid in C17

Forbidden.

_Thread_local is process-wide

One per thread.

7. How to answer storage-class interview questions aloud

A fundamentals-first prompt may ask for the classic four names, automatic locals, or static versus extern. Output prompts may test 13, 16, 19 from next() or the two-file trace. Deeper follow-ups add tentative definitions, recursion, invalid addresses, thread-local state, or object duration versus function linkage.

Use five passes: (1) state the C version, (2) identify object or function and scope, (3) assign object duration (automatic, static or thread), (4) assign no, internal or external linkage, and (5) classify declaration, tentative definition or full definition before tracing values. static int calls; is an object, block scoped, static duration, no linkage, a definition, and zero-initialised once.

For more practice, use C Programming Interview Questions for Freshers: 40 Answered, then use Coding for Placements for broader spoken-answer preparation.

8. Storage classes in 60 seconds, then the next step

Automatic objects are fresh on entry. Block-scope static keeps local scope and lasts for the program. File-scope static gives internal linkage. extern usually declares rather than copies. register gives no hardware guarantee. File-scope objects have static duration without static. Separate scope, duration and linkage.

Reproduce next(): 13, 16, 19; sample(): 7, 7; visits(2), visits(1): 3, 5; and 1 5 1 15, 2 6 2 21, total=21. Continue with C Language Course or browse KnowledgeGate Learn.