Bird
0
0

Identify the error in the following C code snippet:

medium📝 Debug Q14 of 15
C - Variables and Data Types
Identify the error in the following C code snippet:
void func() {
    printf("%d", x);
}
int main() {
    int x = 5;
    func();
    return 0;
}
Ax is not declared globally, so func() cannot access it
BVariable x is used before declaration in func()
CMissing return type for main()
DSyntax error in printf statement
Step-by-Step Solution
Solution:
  1. Step 1: Check variable x usage in func()

    func() tries to print x but x is declared only inside main(), so func() cannot see it.
  2. Step 2: Understand variable scope rules

    Variables declared inside main() are local to main() only, not visible in other functions.
  3. Final Answer:

    x is not declared globally, so func() cannot access it -> Option A
  4. Quick Check:

    Local variables not visible outside their function [OK]
Quick Trick: Functions can't access locals from other functions [OK]
Common Mistakes:
  • Thinking main()'s x is global
  • Ignoring variable scope rules
  • Assuming implicit global variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More C Quizzes