ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
int x = 10;
void foo(void) {
int x = 20;
{
int x = 30;
printf("%d ", x);
}
printf("%d ", x);
}
int main(void) {
foo();
printf("%d", x);
}- A. 20 20 10
- B. 30 20 10
- C. 10 20 30
- D. 30 10 10
Correct answer: B. 30 20 10
The innermost block's x=30 shadows foo's x=20, which shadows the global x=10. Each printf uses the innermost x visible at that point: 30 inside the nested block, 20 after it ends (back in foo's scope), 10 in main (the global).
More C Programming questions
- What is printed? c void f(void) { int x = 5; x = x++ + ++x; printf("%d", x); } int main(void) { f(); }
- What is printed? c int f(void) { printf("f "); return 1; } int g(void) { printf("g "); return 0; } int main(void) { if (g() && f())…
- What is printed? c int f(void) { printf("f "); return 1; } int g(void) { printf("g "); return 1; } int main(void) { if (f() || g())…
- What is printed? c int globalVar = 100; void modify(void) { int globalVar = 200; globalVar += 5; printf("local:%d ", globalVar); } int…
- What is printed? c int a = 3; int result = a++ 2 + --a; printf("%d %d", a, result);
- What is printed? c // header.h (included by both files) extern int sharedValue; c // a.c #include "header.h" int sharedValue = 42; c //…