ProgrammingModerateTCS NQT · Infosys
What is printed?
What is printed?
int a[] = {1, 2, 3, 4, 5};
int *p = a + 1, *q = &a[4];
printf("%td %d %d", q - p, *p + *q, p[2]);- A. 3 7 3
- B. 4 7 4
- C. 3 7 4
- D. 12 7 4
Correct answer: C. 3 7 4
Subtracting pointers into the same array gives the distance in elements, not bytes: 4 − 1 = 3 (12 would be the byte gap with 4-byte int). *p + *q = 2 + 5 = 7, and p[2] means *(p + 2) = a[3] = 4.
More C Programming questions
- With 4-byte int, what is printed? c int a[3][4] = {0}; int (p)[4] = a; int q = &a[0][0]; printf("%td %td %td", (p + 2) - p, (char )(p + 1)…
- What is printed? c int calls = 0; int f(int n) { calls++; return n < 2 ? n : f(n / 2) + f(n - 1); } int main(void) { int r = f(6)…
- Assume 32-bit unsigned int. What is printed? c unsigned x = 0xB8; / 1011 1000 / printf("%u %u %u", x & (x - 1), x | (x - 1), x & -x);
- What is printed? c int f(void) { static int n = 5; return n--; } int main(void) { int s = 0; while (f() > 2) s++; printf("%d %d", s, f()); }
- What is printed? c #define MUL(a, b) a b #define ADD(a, b) a + b printf("%d", MUL(ADD(1, 2), ADD(3, 4)));
- Assume little-endian, 8-bit char and 32-bit unsigned int. What is printed? c union { unsigned int u; unsigned char b[4]; } x; x.u =…