ProgrammingModerateAccenture · Capgemini pattern
What is printed?
What is printed?
struct P { int x, y; } p = {3, 4}, *q = &p;
q->x += q->y;
(*q).y *= 2;
printf("%d %d", p.x, p.y);- A. 3 4
- B. 7 8
- C. 7 4
- D. 3 8
Correct answer: B. 7 8
q points at p itself (not a copy), and q->x is shorthand for (*q).x. So p.x becomes 3 + 4 = 7 and then p.y becomes 4 × 2 = 8. The brackets in (*q).y are needed because . binds tighter than unary *.
More C Programming questions
- 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);
- What is printed? c int tick(int reset) { static int c = 10; if (reset) c = 0; return c += 2; } int main(void) { int a = tick(0); int b =…
- What is printed? c int i, j; for (i = 0, j = 0; i < 5; i++, j += i); printf("%d %d", i, j);
- What is printed? c / LP64: 4-byte int, 8-byte long, 4-byte float / char c = 'a'; short s = 2; printf("%zu %zu %zu %zu", sizeof(c)…
- What is printed? c / x86-64 LP64: natural alignment (double 8, int 4, short 2) / struct X { char a[5]; double d; char e; }; struct Y {…