ProgrammingVery hardCompetitive coding (CP)

What is printed by the first two calls?

What is printed by the first two calls?
#include <stdio.h>
unsigned next(void) {
    static unsigned s = 1;
    s = s * 1103515245u + 12345u;
    return (s >> 16) & 0x7FFFu;
}
int main(void) {
    unsigned a = next(), b = next();
    printf("%u %u", a, b);
}
  1. A. 16838 10113
  2. B. 16838 5758
  3. C. 16838 16838
  4. D. 5758 16838

Correct answer: B. 16838 5758

The static variable keeps its state between calls, and unsigned arithmetic wraps modulo 2^32, so the second value is generated from the updated state. The third output is 10113, so a second call that skipped the update would repeat 16838.

More C Programming questions

Exams that ask this topic