ProgrammingModerateGATE CS · TCS NQT
What is printed? Assume char is 1 byte, int is 4 bytes with 4-byte alignment.
What is printed? Assume char is 1 byte, int is 4 bytes with 4-byte alignment.
#include <stdio.h>
struct S { char a[5]; int b; };
int main(void) {
printf("%zu", sizeof(struct S));
}- A. 12
- B. 9
- C. 8
- D. 16
Correct answer: A. 12
Member b must sit at an offset that is a multiple of 4, so it starts at offset 8 after 3 bytes of padding. The struct is then 12 bytes long.
More C Programming questions
- What is printed? c #include <stdio.h> enum { A, B = 5, C }; int main(void) { printf("%d %d", C, A + B); }
- What is printed? c #include <stdio.h> int main(void) { int i = 0, n = 0; while (i++, i < 5) n++; printf("%d", n); }
- What is printed? c #include <stdio.h> int main(void) { int i = 0, c = 0; do { i++; if (i % 2) continue; c++; } while (i < 6); printf("%d"…
- After sorting by squared distance from the origin, with ties broken by x ascending, what is printed? c #include <stdio.h> #include…
- How many integers x in 1..255 have exactly four set bits? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xDEAD; int c = 0; unsigned t = x & 0xAAAA; while (t) { c += t & 1u; t…