Bird
0
0

Consider the following C code:

hard📝 Application Q15 of 15
C - Variables and Data Types
Consider the following C code:
int x = 1;
void outer() {
    int x = 2;
    {
        int x = 3;
        printf("%d ", x);
    }
    printf("%d ", x);
}
int main() {
    outer();
    printf("%d", x);
    return 0;
}

What is the output when this program runs?
A1 2 3
B3 2 1
C3 3 3
D2 3 1
Step-by-Step Solution
Solution:
  1. Step 1: Analyze innermost block variable

    The innermost block declares x = 3, so first printf prints 3.
  2. Step 2: Analyze outer block variable

    After inner block ends, the x = 3 goes out of scope, so next printf prints outer x = 2.
  3. Step 3: Analyze global variable in main()

    Finally, main prints global x = 1.
  4. Final Answer:

    3 2 1 -> Option B
  5. Quick Check:

    Inner blocks hide outer variables [OK]
Quick Trick: Inner block variables override outer ones [OK]
Common Mistakes:
  • Ignoring block-level scope
  • Assuming all x print same value
  • Confusing order of prints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes