ProgrammingHardGATE CS
What is printed? Assume unsigned int is 32 bits and unsigned char is 8 bits.
What is printed? Assume unsigned int is 32 bits and unsigned char is 8 bits.
#include <stdio.h>
int main(void) {
unsigned int x = 1u << 31;
unsigned int y = x * 2 + (x >> 31);
unsigned char z = (unsigned char)(y + 255);
printf("%u %u", y, z);
return 0;
}- A. 1 255
- B. 0 255
- C. 1 0
- D. 2147483649 0
Correct answer: C. 1 0
x = 2^31, so x * 2 wraps to 0 in unsigned arithmetic, and x >> 31 is 1, giving y = 1. Then y + 255 = 256, and converting to unsigned char keeps the low 8 bits: 0.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int i, j, k = 0; for (i = 0, j = 20; i < j; i += 3, j -= 2) k += j - i; printf("%d…
- What is printed? (sizet is an unsigned type.) c #include <stdio.h> #include <string.h> int main(void) { char s[] = "ab", t[] = "abcd"; if…
- What is printed? c #include <stdio.h> int f(int x) { int r = 0; switch (x) { case 1: r += 1; default: r += 10; case 2: r += 2; break; case…
- What is the exact output? c #include <stdio.h> void p(int n) { if (n <= 0) return; printf("%d", n); p(n - 1); printf("%d", n); p(n - 2); }…
- What is printed? Assume int is 4 bytes and the array is a 2-D array object of automatic storage. c #include <stdio.h> int main(void) { int…
- What is printed? Assume IEEE-754 float (32-bit) and double (64-bit) with no extended-precision evaluation. c #include <stdio.h> int…