ProgrammingHardTCS NQT · Accenture

Assume unsigned int is 32 bits. What is printed?

Assume unsigned int is 32 bits. What is printed?
#include <stdio.h>
int main(void) {
    int x = -17;
    unsigned y = (unsigned)x;
    printf("%u", y % 10u);
}
  1. A. 9
  2. B. 1
  3. C. -7
  4. D. 3

Correct answer: A. 9

Converting -17 to unsigned gives 2^32 - 17 = 4294967279, whose remainder mod 10 is 9. Taking the signed remainder would give -7.

More C Programming questions

Exams that ask this topic