ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned x = 7;
    int i = 0;
    while (x & 1u) { x >>= 1; i++; }
    printf("%d", i);
}
  1. A. 2
  2. B. 4
  3. C. 3
  4. D. 0

Correct answer: C. 3

The loop counts the low bits that are set. 7 = 111 has three of them before the first zero, which is above bit 2.

More C Programming questions

Exams that ask this topic