ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
int x = 5;
void f(void) { int x = 1; x++; }
void g(void) { x++; }
int main(void) {
f(); g(); g();
printf("%d", x);
}- A. 8
- B. 4
- C. 2
- D. 7
Correct answer: D. 7
Inside f the local x shadows the global, so f's increment dies with its local copy; the two g calls raise the global from 5 to 7. 8 assumes f also touched the global, and 4 treats int x = 1 in f as an assignment to the global.
More C Programming questions
- What is printed? c char s = "programming"; int c = 0; while (s) if (s++ == 'm') c++; printf("%d %c", c, (s - 1));
- What is printed? c unsigned x = 0xF0; / 32-bit unsigned / printf("%u %u %u %u", x >> 4, x << 1, x & 0x3C, x | 0x0F);
- What is printed? c int s = 0, i; for (i = 0; i < 10; i++) { if (i % 3) continue; s += i; } printf("%d %d", s, i);
- What is printed? c struct P { int x, y; } p = {3, 4}, q = &p; q->x += q->y; (q).y = 2; printf("%d %d", p.x, p.y);
- What is printed? c int m[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; int s = 0; for (int i = 0; i < 3; i++) s += m[i][2 - i]; printf("%d %d", s…
- What is printed? c int a[5] = {1, 2, 3, 4, 5}; int p = a + 4; while (p > a) printf("%d", --p);