ProgrammingVery hardCompetitive coding (CP)

What is printed? Assume uint32t is 32 bits.

What is printed? Assume uint32_t is 32 bits.
#include <stdint.h>
#include <stdio.h>
int main(void) {
    uint32_t h = 5381;
    const char *s = "ab";
    for (; *s; s++)
        h = h * 33u + (unsigned char)*s;
    printf("%u", h);
}
  1. A. 5863208
  2. B. 5863207
  3. C. 177670
  4. D. 5863216

Correct answer: A. 5863208

The djb2 update h = 33h + c runs over a and b: 5381 * 33 + 97 = 177670, then 177670 * 33 + 98 = 5863208. The value stays inside 32 bits, so there is no wrap-around.

More C Programming questions

Exams that ask this topic