ProgrammingHardProduct company OA · GATE CS
Assume 2-byte short, 4-byte int and natural alignment. What is printed?
Assume 2-byte short, 4-byte int and natural alignment. What is printed?
#include <stddef.h>
struct R { char a; short b; char c; int d; };
printf("%zu %zu %zu", offsetof(struct R, b), offsetof(struct R, d), sizeof(struct R));- A. 2 5 9
- B. 2 8 16
- C. 2 8 12
- D. 1 4 8
Correct answer: C. 2 8 12
a sits at 0 and b must start at an even offset, so 1 byte of padding puts b at 2–3. c is at 4, d needs a multiple of 4, so 3 bytes of padding put it at 8–11. The total, 12, is already a multiple of 4. '2 5 9' ignores d's alignment.
More C Programming questions
- What is printed? c int sum(int (r)[3], int n) { int s = 0; for (int i = 0; i < n; i++) s += r[i][i % 3]; return s; } int main(void) { int…
- What is printed? c int even(int n); int odd(int n) { return n == 0 ? 0 : even(n - 1); } int even(int n) { return n == 0 ? 1 : odd(n - 1)…
- What is printed? c char w[] = {"pear", "fig", "apple", "kiwi"}; for (int i = 0; i < 3; i++) for (int j = 0; j < 3 - i; j++) if…
- What is printed? c int score = 73; char g = score >= 90 ? 'A' : score >= 75 ? 'B' : score >= 60 ? 'C' : 'D'; printf("%c%d", g, g - 'A');
- Which of these statements about C (not C++) are true? (i) A const int variable at file scope is a constant expression, so it can size a…
- Which of these is NOT allowed for a variable declared with the register storage class in C?