ProgrammingVery hardGATE CS · TCS NQT
What is printed? Assume int is 4 bytes with 4-byte alignment.
What is printed? Assume int is 4 bytes with 4-byte alignment.
#include <stdio.h>
union U { char c[5]; int i; };
int main(void) {
printf("%zu", sizeof(union U));
}- A. 5
- B. 4
- C. 8
- D. 12
Correct answer: C. 8
A union is as large as its largest member, rounded up to the alignment of the most aligned member. The five-byte char array is padded to 8, the next multiple of the int alignment 4.
More C Programming questions
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { printf("%d", strcmp("apple", "apricot") < 0); }
- How many times does the letter a occur in "panda"? The program counts with recursion. c #include <stdio.h> int cnt(const char s) { if (s…
- What is printed by this recursive Pascal triangle entry? c #include <stdio.h> int C(int n, int k) { if (k == 0 || k == n) return 1; return…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; for (int i = 0; i < 5; i++) { switch (i) { case 2: break; default: c++…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; for (int i = 0; i < 5; i++) { switch (i) { case 1: continue; } c++; }…
- What is printed? c #include <stdio.h> int main(void) { int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; printf("%d", ((m + 1) + 2)); }