ProgrammingHardProduct company OA · GATE CS
Assume 4-byte int, 8-byte double and natural alignment. What is printed?
Assume 4-byte int, 8-byte double and natural alignment. What is printed?
struct T { double d; int i; };
struct T arr[3];
printf("%td", (char *)(arr + 2) - (char *)arr);- A. 26
- B. 24
- C. 2
- D. 32
Correct answer: D. 32
struct T needs 12 bytes of members but is padded to 16 so that the double in the next array element stays 8-byte aligned. arr + 2 is two elements on: 2 × 16 = 32 bytes. '24' forgets the tail padding; '2' forgets the char casts.
More C Programming questions
- What is printed? c int len(const char s) { return s ? 1 + len(s + 1) : 0; } int cnt(const char s, char c) { return !s ? 0 : (s == c) +…
- What is printed? c int dr(int n) { return n < 10 ? n : dr(n / 10 + n % 10); } int main(void) { printf("%d %d", dr(9875), dr(99999)); }
- What is printed? c unsigned x = 0xF0F0; x = x - ((x >> 1) & 0x5555); x = (x & 0x3333) + ((x >> 2) & 0x3333); x = (x + (x >> 4)) & 0x0F0F…
- What is printed? c struct N { int v, next; } t[] = {{10, 2}, {20, -1}, {30, 4}, {40, 1}, {50, 1}}; int s = 0; for (int i = 0; i != -1; i =…
- Given int x; const int pc; int p; int const cp = &x;, which assignment is a constraint violation that the compiler must diagnose?
- What is printed? c #define S(x) #x printf("%zu %zu", strlen(S("hi")), sizeof(S(ab)));