ProgrammingVery hardTCS NQT · Infosys

What is printed? Assume unsigned int is 32 bits.

What is printed? Assume unsigned int is 32 bits.
#include <stdio.h>
int main(void) {
    unsigned long long n = 4294967290ULL;
    unsigned b = 10;
    unsigned a = (unsigned)n;
    printf("%u", (a + b - 1) / b);
}
  1. A. 429496729
  2. B. 0
  3. C. 429496730
  4. D. 1

Correct answer: B. 0

a + b - 1 = 4294967299, which wraps modulo 2^32 to 3, and 3 / 10 = 0. The intended ceiling division is wrong after the overflow.

More C Programming questions

Exams that ask this topic