ProgrammingVery hardGATE CS
What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a multiple of its largest alignment.
What is printed? Assume the usual ABI: char 1 byte, short 2, int 4, with each member aligned to its own size and the struct padded to a multiple of its largest alignment.
#include <stdio.h>
struct A { char c; int i; char d; };
struct B { int i; char c; char d; };
struct C { char c; char d; short s; int i; };
int main(void) {
printf("%zu %zu %zu", sizeof(struct A), sizeof(struct B), sizeof(struct C));
return 0;
}- A. 12 8 8
- B. 12 8 12
- C. 8 8 8
- D. 12 12 8
Correct answer: A. 12 8 8
struct A: c at 0, padding to 4, i at 4, d at 8, tail padding to 12. struct B: i at 0, c at 4, d at 5, padded to 8. struct C: c at 0, d at 1, s at 2, i at 4 gives 8 with no padding.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcd"; char p = s; (p)++; p++ += 2; ++p; p[1]--; printf("%s", s)…
- What is printed? Assume C99 or later (division truncates towards zero). c #include <stdio.h> int main(void) { printf("%d %d %d %d", -17 /…
- What is printed? Assume unsigned int is 32 bits and unsigned char is 8 bits. c #include <stdio.h> int main(void) { unsigned int x = 1u <<…
- What is printed? c #include <stdio.h> int main(void) { int i, j, k = 0; for (i = 0, j = 20; i < j; i += 3, j -= 2) k += j - i; printf("%d…
- What is printed? (sizet is an unsigned type.) c #include <stdio.h> #include <string.h> int main(void) { char s[] = "ab", t[] = "abcd"; if…
- What is printed? c #include <stdio.h> int f(int x) { int r = 0; switch (x) { case 1: r += 1; default: r += 10; case 2: r += 2; break; case…