ProgrammingVery hardTCS NQT · Infosys
Assume int is 4 bytes with 4-byte alignment and char is 1 byte. What is printed?
Assume int is 4 bytes with 4-byte alignment and char is 1 byte. What is printed?
#include <stdio.h>
struct A { char c; int i; char d; };
struct B { int i; char c; char d; };
int main(void) {
printf("%zu %zu", sizeof(struct A), sizeof(struct B));
}- A. 12 8
- B. 9 8
- C. 12 6
- D. 8 8
Correct answer: A. 12 8
Struct A pads after c and after d to reach 4-byte alignment, giving 12 bytes. Struct B fills its 4 bytes with i, then c and d fit in the next 2 bytes and the tail pads to 8.
More C Programming questions
- How many subsets of {1, 2, 3, 4} (the empty set included) have an XOR of zero? c #include <stdio.h> int main(void) { int count = 0; for…
- What is printed? Assume int is 32 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t b = (uint8t)-200; printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { char s[] = "abcdef"; char p = s; int n = 0; while (p && p - s < 4) n += p++ - 'a'…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = (int const )a; const int y =…
- What is printed? c #include <stdio.h> int main(void) { int m[3][3]; for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) m[i][j] = i 3…
- How many times is disk 3 moved in this recursive Towers of Hanoi with 5 disks? Compute the program output. c #include <stdio.h> int cnt =…