ProgrammingHardIBM · Deloitte pattern
Assume 32-bit int. What is printed?
Assume 32-bit int. What is printed?
int a = -1;
unsigned int b = a;
printf("%u", b);- A. -1
- B. 0
- C. 1
- D. 4294967295
Correct answer: D. 4294967295
Converting a negative int to unsigned int is well-defined: the standard requires adding 2^32 (modulo arithmetic) until the value fits in the unsigned range. -1's bit pattern is all 1s, which as an unsigned 32-bit value is 4294967295 (2^32 - 1).
More C Programming questions
- What is printed? (Assume no overflow trapping.) c int f(int n) { if (n == 0) return 0; return n + f(n - 1); } int main(void) {…
- What is printed? c int mystery(int a, int b) { if (b == 0) return a; return mystery(b, a % b); } int main(void) { printf("%d", mystery(48…
- What is printed? (Assume int is 32 bits, two's complement.) c unsigned int a = 0xF0F0F0F0; unsigned int b = a >> 4; printf("%X", b);
- What is printed? (Assume int is 32 bits, two's complement, arithmetic right shift for signed negatives — the common but…
- What is printed? (Check if a number is a power of two.) c int isPowerOfTwo(int n) { return n > 0 && (n & (n - 1)) == 0; } int main(void) {…
- What is printed? c int swaptrick(int a, int b) { a = a ^ b; b = a ^ b; a = a ^ b; printf("%d %d ", a, b); return a + b; } int main(void) {…