Bird
0
0

What will be the output of this C program?

medium📝 Predict Output Q5 of 15
C - Variables and Data Types
What will be the output of this C program?
int x = 1;
void test() {
    x = 5;
    int x = 10;
    printf("%d ", x);
}
int main() {
    test();
    printf("%d", x);
    return 0;
}
A5 1
B10 5
C5 10
D10 1
Step-by-Step Solution
Solution:
  1. Step 1: Understand variable shadowing and assignment

    Inside test(), 'int x = 10;' declares a new local x, shadowing global x. The assignment 'x = 5;' before local declaration affects global x.
  2. Step 2: Trace output values

    printf inside test() prints local x = 10. After test(), global x was set to 5, so main prints 5.
  3. Final Answer:

    10 5 -> Option D
  4. Quick Check:

    Local shadows after assignment; global x changed = 10 5 [OK]
Quick Trick: Local variables declared after assignment don't affect earlier assignments [OK]
Common Mistakes:
  • Assuming assignment affects local x before declaration
  • Confusing which x is printed
  • Ignoring order of statements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes