ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdint.h>
#include <stdio.h>
uint16_t sat(uint16_t a, uint16_t b) {
    uint32_t p = (uint32_t)a * b;
    return p > 65535u ? 65535 : (uint16_t)p;
}
int main(void) {
    printf("%d %d", sat(300, 300), sat(3, 4));
}
  1. A. 65535 12
  2. B. 65535 90000
  3. C. 0 12
  4. D. 12 65535

Correct answer: A. 65535 12

300 * 300 = 90000 is larger than 65535, so the result saturates to 65535, while 3 * 4 = 12 is returned unchanged.

More C Programming questions

Exams that ask this topic