ProgrammingHardIBM · Deloitte pattern
On a little-endian machine with 4-byte int and ASCII, what is printed?
On a little-endian machine with 4-byte int and ASCII, what is printed?
union { int i; char c; } u;
u.i = 0x41424344;
printf("%c", u.c);- A. A
- B. D
- C. A garbage character
- D. B
Correct answer: B. D
All union members start at the same address, so u.c reads the first byte of i. Little-endian stores the least significant byte first, 0x44, which is 'D'. A big-endian machine would print 'A'.
More C Programming questions
- What is printed? c void swap(int x, int y) { x ^= y; y ^= x; x ^= y; } ... int a = 7; swap(&a, &a); printf("%d", a);
- On a two's-complement machine, what is printed? c printf("%d %d", 40 & -40, 40 & (40 - 1));
- For a signed int n on a two's-complement machine, how many of these are correct? (i) (n & (n - 1)) == 0 is true for every positive power…
- What is printed? c #define DOUBLE(x) x + x printf("%d", 10 / DOUBLE(5) 2);
- Which of a, b, c, d are pointers? c #define IPTR int typedef int IPT; IPTR a, b; IPT c, d;
- What is printed? c #define DEBUG 0 ... #ifdef DEBUG printf("on "); #endif #if DEBUG printf("trace"); #endif