ProgrammingModerateTCS NQT · Wipro

What is printed?

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

Correct answer: A. 3

40 = 101000 in binary, which has three trailing zeros. Counting the set bits instead would give 2.

More C Programming questions

Exams that ask this topic