ProgrammingHardTCS NQT · Infosys pattern
Assume a 64-bit LP64 ABI. What is printed?
Assume a 64-bit LP64 ABI. What is printed?
typedef union {
struct { unsigned char r, g, b, a; } channels;
unsigned int packed;
} Color;
Color c;
c.channels.r = 255;
c.channels.g = 0;
c.channels.b = 0;
c.channels.a = 255;
printf("%zu", sizeof(Color));- A. 4
- B. 16
- C. 8
- D. 1
Correct answer: A. 4
The union's two members are the anonymous struct (4 unsigned chars, 4 bytes total, no padding needed since all members are 1-byte-aligned) and packed (a 4-byte unsigned int). Both members are 4 bytes, so the union itself is 4 bytes — a classic technique for viewing 4 packed color channels as one 32-bit integer.
More C Programming questions
- What is printed? c #define SQUARE(x) x x int main() { int a = 5; printf("%d", SQUARE(a + 1)); }
- What is printed? c #define INCR(x) x++ int main() { int i = 5; int r = 2 INCR(i); printf("%d %d", r, i); }
- What is printed? c #define PR(x) printf(#x " = %dn", x) int main() { int value = 42; PR(value); }
- What is printed? c #define CONCAT(a, b) a##b int main() { int xy = 100; printf("%d", CONCAT(x, y)); }
- What is printed? c #define ISEVEN(n) ((n) % 2 == 0) int main() { int x = 7; if (!ISEVEN(x)) printf("odd"); else printf("even"); }
- Assume 32-bit int. What is printed? c int flags = 0; flags |= (1 << 2); flags |= (1 << 4); flags &= ~(1 << 2); printf("%d", flags);