ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint32_t a = 65536, b = 65536;
    uint32_t p = a * b;
    printf("%u %s", p, (b != 0 && p / b != a) ? "overflow" : "ok");
}
  1. A. 4294967296 ok
  2. B. 0 ok
  3. C. 0 overflow
  4. D. 65536 overflow

Correct answer: C. 0 overflow

2^16 times 2^16 = 2^32, which wraps to 0 in a 32-bit unsigned type. Dividing back gives 0, which differs from a, so the overflow is detected.

More C Programming questions

Exams that ask this topic