ProgrammingHardProduct-company MCQ pattern
What is printed?
What is printed?
/* 4-byte int with 4-byte alignment */
struct S { int a; char b; } arr[5];
printf("%td %td", &arr[4] - &arr[1],
(char *)&arr[4] - (char *)&arr[1]);- A. 3 15
- B. 24 24
- C. 3 3
- D. 3 24
Correct answer: D. 3 24
Subtracting two struct S pointers gives the distance in ELEMENTS: 4 − 1 = 3. Each struct S is padded from 5 bytes to 8 so that the int in the next array element stays 4-byte aligned, so the same distance measured with char pointers is 3 × 8 = 24 bytes. 15 assumes no tail padding.
More C Programming questions
- What is printed? c char s[] = "abcdef"; char p = s + 2; p[1] = 'X'; printf("%s %s %c %zu", s, p, (p - 2), strlen(p + 4));
- What is printed? c int f(int n) { if (n <= 0) return 0; return n % 2 ? f(n - 1) + n : f(n / 2) 2; } int main(void) { printf("%d %d"…
- What is printed? c unsigned char x = 0x1D, r = 0; for (int i = 0; i < 8; i++) { r = (r << 1) | (x & 1); x >>= 1; } printf("%#x", r);
- What is printed? c int a = 2, b = 3, c = 4; printf("%d %d %d", a + b c % 5 - -a, a < b < c, c > b > a);
- What is printed? c char w[] = {"red", "green", "blue"}; char p = w + 1; printf("%c %s %c %zu", p, (p + 1) + 2, ((p - 1) + 1), strlen(p[1])…
- What is printed? c struct { unsigned a : 3; unsigned b : 5; } f = {0, 0}; f.a = 9; f.b = 33; printf("%u %u ", f.a, f.b); f.a += 7; f.b =…