ProgrammingHardWipro · Cognizant pattern
What is printed? (Assume int is 32 bits.)
What is printed? (Assume int is 32 bits.)
int a = -1;
unsigned int b = (unsigned int)a;
printf("%u", b);- A. 4294967295
- B. -1
- C. 0
- D. 2147483647
Correct answer: A. 4294967295
-1 in two's complement 32-bit representation is all 1-bits; reinterpreting that same bit pattern as unsigned int gives the maximum unsigned 32-bit value, 4294967295 (UINT_MAX) — this conversion is well-defined (unlike signed overflow), per the standard's modulo-2^n rule for signed-to-unsigned conversion.
More C Programming questions
- What is printed? (Check if two integers have opposite signs, without using multiplication.) c int oppositeSigns(int a, int b) { return (a…
- What is printed? (Integer overflow on multiplication.) c #include <limits.h> int main(void) { int a = INTMAX; int b = 2; int result = a b…
- What happens? c int arr[3] = {1, 2, 3}; int p = arr; p += 10; p = 99; printf("done");
- What happens? c int compare(const void a, const void b) { return (int )a - (int )b; } int main(void) { int arr[] = {5, 2, 8, 1, 9}…
- Assume 32-bit int. What is printed? c unsigned int a = 0; a--; printf("%u", a);
- Assume 32-bit int. What is printed? c int flags = 0b1010; int mask = 0b0110; printf("%d %d", flags & mask, flags | mask);