ProgrammingVery hardCompetitive coding (CP)

What is printed? Assume unsigned int is 32 bits and unsigned long long is 64 bits.

What is printed? Assume unsigned int is 32 bits and unsigned long long is 64 bits.
#include <stdio.h>
unsigned long long pw(unsigned b, unsigned e) {
    return e == 0 ? 1 : b * pw(b, e - 1);
}
int main(void) {
    printf("%llu", pw(3, 40));
}
  1. A. 12157665459056928801
  2. B. 689956897
  3. C. 4052555153018976267
  4. D. 12157665459056928802

Correct answer: A. 12157665459056928801

b * pw(...) is computed in unsigned long long because pw returns that type, so 3^40 = 12157665459056928801 fits in 64 bits. Doing the product in 32 bits would give 3^40 mod 2^32 = 689956897.

More C Programming questions

Exams that ask this topic