ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int x = 100;
void f(void) {
int x = 5;
{
int x = 1;
printf("%d ", x);
}
printf("%d ", x);
}
int main(void) {
f();
printf("%d", x);
}- A. 1 5 5
- B. 5 100 1
- C. 1 100 100
- D. 1 5 100
Correct answer: D. 1 5 100
Each block introduces a new x that shadows the outer one within its own scope. The innermost block prints its x=1; once that block ends, the enclosing one sees its own x=5; main's x (the global, 100) is untouched by f's locals.
More C Programming questions
- What is printed? c int x = 5; int f(void) { int x = x + 1; return x; } int main(void) { printf("%d", f()); }
- What is printed? c int f(void) { static int x; return x++; } int main(void) { printf("%d %d %d", f(), f(), f()); }
- What is printed? c static int helper(int n) { return n 2; } int main(void) { printf("%d", helper(5)); }
- What is printed? c void f(void) { static int x = 0; x += 10; printf("%d ", x); } int main(void) { for (int i = 0; i < 3; i++) f(); }
- What is printed? c int global = 1; void modify(void) { int global = 2; global++; } int main(void) { modify(); printf("%d", global); }
- What is printed? c #include <stdio.h> int x; // tentative definition, file scope int x; // another tentative definition, same file int…