Bird
0
0

Consider this code snippet:

hard📝 Application Q8 of 15
C - Variables and Data Types
Consider this code snippet:
int x = 5;
void change() {
    int x = 10;
    {
        extern int x;
        x = 20;
    }
}
int main() {
    change();
    printf("%d", x);
    return 0;
}

What will be the output and why?
A10, because local x inside change() is printed
B20, because extern refers to global x and changes it
C5, because global x is unchanged
DCompilation error due to extern inside block
Step-by-Step Solution
Solution:
  1. Step 1: Understand extern usage inside block

    extern int x; inside inner block refers to global x, not local x in change().
  2. Step 2: Trace assignment and output

    Assignment x = 20 modifies global x. main prints global x, which is now 20.
  3. Final Answer:

    20, because extern refers to global x and changes it -> Option B
  4. Quick Check:

    extern inside block accesses global variable [OK]
Quick Trick: extern inside block accesses global variable, not local [OK]
Common Mistakes:
  • Assuming extern refers to local variable
  • Thinking local x is printed
  • Believing global x is unchanged

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes