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);
}- A. 16838 10113
- B. 16838 5758
- C. 16838 16838
- 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
- What is printed? c #include <stdio.h> int main(void) { int total = 0; for (unsigned x = 1; x <= 100; x++) { unsigned y = x; while (y) {…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { const char names[] = {"ann", "bob", "cat", "dan", "eve"}; const…
- What is printed? Assume long long is 64 bits and that the product fits in a long long. c #include <stdio.h> int main(void) { long long p =…
- What is printed? c #include <stdio.h> int clz16(unsigned short x) { int n = 0; while (!(x & 0x8000u)) { x <<= 1; n++; } return n; } int…
- What is printed? Assume ASCII character encoding. c #include <stdio.h> int main(void) { int d = 'z' - 'a'; char e = 'A' + d 2; printf("%d…
- What is printed? c #include <stdio.h> int main(void) { int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = m; int q = (int )p + 4…