Storage Classes Questions with Answers

31 practice questions on Storage Classes (C) with answers and step-by-step solutions. Try 10 samples below, then practise all 31 free in the AptiNest app.

Sample questions

Question 1Easy

What is the initial value of a local (automatic) int variable declared without an initialiser, such as int x; inside a function?

  1. Always 0
  2. Always -1
  3. The value it had in the previous call
  4. Indeterminate: reading it before assignment is a bug

Answer: D. Indeterminate: reading it before assignment is a bug

Explanation

Automatic variables are not initialised; their value is indeterminate, and using it gives unpredictable results. Only static-duration objects are zero-initialised.

Step-by-step solution

  1. Local variables declared without initialiser have no default value.
  2. Their value depends on memory contents, which is unpredictable.
  3. Reading such a variable before assignment leads to undefined behaviour.
  4. This makes the value indeterminate and unsafe to use.
  5. Thus, using it before assigning is considered a bug.
  6. Indeterminate: reading it before assignment is a bug.

Why the other options are wrong

  • A. Zero is only guaranteed for static or global variables; a plain local int has no default value.
  • B. Minus one is not a rule in C; automatic variables have indeterminate values, not -1.
  • C. A local is recreated on each call and keeps no value from before; its value is indeterminate.

Tip: Always initialise local variables to avoid undefined behaviour.

Question 2Easy

What is the output?

#include <stdio.h>

int g;

int main(void) {
    static int s;
    printf("%d %d\n", s, g);
    return 0;
}
  1. 0 0
  2. Compilation error
  3. Garbage values
  4. 0 followed by a garbage value

Answer: A. 0 0

Explanation

Objects with static storage duration (globals and static locals) that have no initialiser are zero-initialised.

Step-by-step solution

  1. Globals and static local variables without an initialiser are zero-initialised before main runs.
  2. So g and the static local s both start at 0.
  3. Printf prints 0 0, which is option A.

Why the other options are wrong

  • B. This is valid C, so nothing fails to compile.
  • C. Garbage values come from unset automatic variables, not from static ones.
  • D. Both values are 0, so there is no garbage value.

Tip: Static storage duration (globals and static locals) means a zero start value unless you set one.

Question 3Easy

What is the output?

#include <stdio.h>

int main(void) {
    int x = 1;
    {
        int x = 2;
        printf("%d ", x);
    }
    printf("%d\n", x);
    return 0;
}
  1. 2 1
  2. 2 2
  3. 1 1
  4. Compilation error

Answer: A. 2 1

Explanation

The inner block declares a new x that hides the outer one only inside that block; after the block, the outer x (1) is visible again.

Step-by-step solution

  1. Inner block sets x = 2 and prints it.
  2. Outer x remains 1 and is printed next.
  3. Final output is 2 followed by 1.
  4. Answer: 2 1.

Why the other options are wrong

  • B. The inner x does not change the outer x, so the final print is 1, not 2.
  • C. The inner block prints its own x, which is 2, so 1 for the first value is wrong.
  • D. Compilation error is wrong; declaring a new x in an inner block is legal shadowing.

Tip: Inner variable hides outer one, but outer reappears after block.

Question 4Easy

What is the output?

#include <stdio.h>

int counter;

void inc(void) {
    counter++;
}

int main(void) {
    inc();
    inc();
    inc();
    printf("%d\n", counter);
    return 0;
}
  1. 0
  2. 1
  3. 3
  4. Garbage value

Answer: C. 3

Explanation

counter is a global that starts at 0 (zero-initialised) and is shared by all functions, so three calls make it 3.

Step-by-step solution

  1. Global variable counter starts at 0 because it's not initialized.
  2. inc() increments counter by 1 each time it is called.
  3. Three calls to inc() add 1 + 1 + 1 = 3 to counter.
  4. printf prints the final value of counter, which is 3.
  5. Final answer: 3.

Why the other options are wrong

  • A. Zero would mean inc never ran; it is called three times, and each call adds one to counter.
  • B. One is wrong; counter is incremented once per call, and there are three calls.
  • D. Garbage is wrong; global variables are zero-initialised, so counter starts at 0.

Tip: Remember global variables start at zero and are shared across functions.

Question 5Medium

What is the output of this C code?

 #include <stdio.h>
 void m();
 void main()
 {
   m();
   m();
 }
 void m()
 {
   static int x = 5;
   x++;
   printf("%d", x);
 }
  1. 6 7
  2. 6 6
  3. 5 5
  4. 5 6

Answer: A. 6 7

Explanation

static int x = 5;

In this statement variable x is a static.
We know that static is initialise only one time. it will reassign the value But reinitialization not Occur.
X++ in m() will the 6. And X++ in n() will be 7. So output is 6 7.

Step-by-step solution

  1. X is static, so it keeps its value between calls to m().
  2. It starts at 5 when it is first set.
  3. First call: x++ makes x 6, and printf prints 6.
  4. Second call: x++ makes x 7, and printf prints 7; the format has no space, so the digits run together.
  5. The printed values are 6 then 7, so the answer is 6 7.

Why the other options are wrong

  • B. The second call makes x 7, not 6.
  • C. The static variable keeps its value, so 5 is never printed.
  • D. 5 is the starting value; the first printed value is 6.

Tip: A static local keeps its value between function calls.

Question 6Medium

Register storage class can be specified to global variables?

  1. true
  2. false
  3. Depends on the compiler
  4. Depends on the standard

Answer: B. false

Explanation

Register should only be used for variables that require quick access - such as counters. so it can not specified at global.

Step-by-step solution

  1. The register storage class asks the compiler to keep a variable in a register.
  2. Global variables last for the whole program, and C does not allow register on file-scope variables.
  3. So the answer is false.

Why the other options are wrong

  • A. Register is not allowed on global variables, so true is wrong.
  • C. The rule is fixed by the standard, not by the compiler.
  • D. The standard forbids it, so the answer does not depend on the standard.

Tip: Register applies only to local variables and function parameters.

Question 7Medium

What is the output?

#include <stdio.h>

int next(void) {
    static int x = 5;
    x += 5;
    return x;
}

int main(void) {
    int a = next();
    int b = next();
    printf("%d %d\n", a, b);
    return 0;
}
  1. 10 10
  2. 15 15
  3. 10 15
  4. 5 10

Answer: C. 10 15

Explanation

The initialiser = 5 runs only once (before program start). The first call makes x 10, the second call adds 5 again to give 15.

Step-by-step solution

  1. static x keeps its value between calls.
  2. First call: x = 10. Second call: x = 15.
  3. Output: 10 15.

Why the other options are wrong

  • A. x is not reset each call.
  • B. Each call adds 5.
  • D. x starts at 5 but is updated before returning.

Tip: Static = remembers.

Question 8Medium

Which statement about the register storage class in C is correct?

  1. register variables are zero-initialised
  2. register can be used for global variables
  3. The address of a register variable cannot be taken with the & operator
  4. The variable is guaranteed to be kept in a CPU register

Answer: C. The address of a register variable cannot be taken with the & operator

Explanation

register is only a hint that the compiler may ignore, but C forbids applying & to such an object. It is allowed only for block-scope objects and parameters.

Step-by-step solution

  1. Register is only a hint, and the compiler may ignore it.
  2. C forbids applying the address operator & to a register variable.
  3. So the correct statement is option C.

Why the other options are wrong

  • A. Automatic variables, including register ones, are not zero-initialised; their starting value is indeterminate.
  • B. Register is allowed only on block-scope variables and parameters, not on globals.
  • D. The compiler may ignore register, so it is not guaranteed to be in a CPU register.

Tip: If you need the address of a variable, do not declare it register.

Question 9Hard

Which storage-class specifier gives each thread its own separate instance of a variable (C11 and later)?

  1. _Thread_local
  2. static
  3. register
  4. volatile

Answer: A. _Thread_local

Explanation

_Thread_local (thread_local via <threads.h>) gives thread storage duration: one instance per thread, alive for that thread's lifetime.

Step-by-step solution

  1. _Thread_local creates a variable unique to each thread.
  2. Other options like static or register do not provide separate instances per thread.
  3. Volatile only affects visibility, not storage duration.
  4. C11 introduced _Thread_local for thread-specific variables.
  5. Correct answer is _Thread_local.

Why the other options are wrong

  • B. Static gives one shared copy for the whole program, not a separate instance for each thread.
  • C. Register is a storage hint for a fast CPU slot; it gives no separate per-thread instance.
  • D. Volatile tells the compiler the value may change unexpectedly; it does not create per-thread copies.

Tip: _Thread_local is needed for thread-local storage in C11 and later.

Question 10Hard

What is the output?

#include <stdio.h>

int x = 10;

int main(void) {
    int x = 20;
    {
        extern int x;
        printf("%d ", x);
    }
    printf("%d\n", x);
    return 0;
}
  1. Compilation error
  2. 10 10
  3. 10 20
  4. 20 20

Answer: C. 10 20

Explanation

The block-scope extern int x refers to the object with external linkage, i.e. the global x = 10, hiding main's local x inside that block. Outside the block the local x = 20 is visible.

Step-by-step solution

  1. Inside main, the local x = 20 hides the file-scope x in the outer block.
  2. Inside the inner block, extern int x refers to the object with external linkage, the file-scope x = 10.
  3. The first printf prints 10 and the second prints the local x = 20, giving 10 20: option C.

Why the other options are wrong

  • A. The code compiles; a block-scope extern declaration is valid and names the global.
  • B. The second print is outside the block, where the local x = 20 is visible.
  • D. The inner extern does not refer to the local x, so the first value is 10, not 20.

Tip: A block-scope extern declaration names the file-scope object, even when a local with the same name is in view.

Practise all 31 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all C topics · All topics