ProgrammingModerateCompetitive coding (CP)

What is printed?

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

Correct answer: D. 1

Masking with 7 is the same as taking the value modulo 8, and 11 steps of 3 add 33, which is 1 modulo 8. Ten steps would give 6 and nine steps would give 3.

More C Programming questions

Exams that ask this topic