ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int pc(unsigned n) {
    return n ? (int)(n & 1u) + pc(n >> 1) : 0;
}
int main(void) {
    printf("%d", pc(1023));
}
  1. A. 9
  2. B. 11
  3. C. 10
  4. D. 1023

Correct answer: C. 10

Each call adds the low bit and recurses on n >> 1. The value 1023 has ten 1 bits.

More C Programming questions

Exams that ask this topic