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>
unsigned f(unsigned n) {
    unsigned r = 1;
    for (unsigned i = 2; i <= n; i++) r *= i;
    return r;
}
int main(void) {
    printf("%u", f(13));
}
  1. A. 1932053503
  2. B. 1932053504
  3. C. 6227020800
  4. D. 1932053505

Correct answer: B. 1932053504

13! = 6227020800 exceeds 2^32 = 4294967296, and unsigned multiplication keeps the value modulo 2^32, giving 1932053504. The value 6227020800 cannot be stored in unsigned int.

More C Programming questions

Exams that ask this topic