ProgrammingModerateCompetitive coding (CP)

Assume unsigned int is 32 bits. What is printed?

Assume unsigned int is 32 bits. What is printed?
#include <stdio.h>
int main(void) {
    unsigned n = 0x1FFFF, p = 1;
    while (p * 2 <= n) p *= 2;
    printf("%u", p);
}
  1. A. 131072
  2. B. 65536
  3. C. 32768
  4. D. 131071

Correct answer: B. 65536

The loop doubles p while the next power still fits under n = 131071, so it stops at 2^16 = 65536. The next power 131072 exceeds n.

More C Programming questions

Exams that ask this topic