Predict the output of the following C Code
int x = 0; int f() { int g() { int x = 1; return f(); } int main() { printf('%d', g()); printf('/n'); getchar(); }The code (cleaned up) is:
int x = 0;
int f() { return x; }
int g() { int x = 1; return f(); }
int main() { printf("%d", g()); }C uses static (lexical) scoping. Inside f(), the name x refers to the global x, which is 0. The local x = 1 inside g() is not visible to f().
So g() returns f() = 0, and the output is 0.
Answer: (B) 0
Reinforce your conceptual grasp with related questions from MAH MCA CET 2024:
Take timed full-length mock tests with authentic question distributions, negative marking (+12/-3 in Math), and instant All India Rank calculation.
Start Free Mock Test Series →