ProgrammingHardProduct company OA
What is printed?
What is printed?
struct P { int x; int y; };
struct P a[3] = {{1, 2}, {3, 4}, {5, 6}};
struct P *p = a;
int r1 = (p + 2)->x;
int r2 = (++p)->y;
int r3 = p[1].x - p[-1].y;
printf("%d %d %d", r1, r2, r3);- A. 5 4 1
- B. 3 4 3
- C. 5 2 3
- D. 5 4 3
Correct answer: D. 5 4 3
p + 2 is &a[2], so r1 = 5 (p itself does not move). ++p moves p to a[1] before the access, so r2 = 4. Now p[1] is a[2] and p[-1] is a[0]: 5 − 2 = 3. '5 2 3' treats ++p like p++.
More C Programming questions
- With a C99 (or later) compiler, what is printed? c int ds(int n) { return n == 0 ? 0 : n % 10 + ds(n / 10); } int main(void) {…
- What is printed? c int f(unsigned x) { int c = 0; while (x) { c += x & 1; x >>= 2; } return c; } int main(void) { printf("%d", f(0xFF) +…
- What is printed? c #define VAR(n) v##n #define PRINT(n) printf("%d ", VAR(n)) int v1 = 10, v2 = 20, v12 = 30; PRINT(1); PRINT(1 + 1)…
- Assume 32-bit unsigned int. What is printed? c unsigned int n = 5; int count = 0; for (unsigned int i = n; i < n + 3; i -= 2) count++…
- What is printed? c char w[][6] = {"red", "green", "blue"}; printf("%zu %zu %zu", sizeof(w), sizeof(w[1]), strlen(w[1]));
- What is printed? c int m = 0; long p(long b, int e) { if (e == 0) return 1; long h = p(b, e / 2); m++; if (e % 2) { m++; return h h b; }…