ProgrammingModerateGATE CS · TCS NQT

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned s = 0;
    for (unsigned b = 1; b; b <<= 1)
        s += 1;
    printf("%u", s);
}
  1. A. 31
  2. B. 32
  3. C. 33
  4. D. 64

Correct answer: B. 32

b takes the 32 values 1, 2, 4, ..., 2^31, and the next shift wraps it to 0, so the loop body runs 32 times.

More C Programming questions

Exams that ask this topic