ProgrammingHardProduct company OA

Assume 8-bit unsigned char. What is printed?

Assume 8-bit unsigned char. What is printed?
int n = 0;
for (unsigned char c = 0; c < 300; c++) {
    n++;
    if (n > 1000) break;
}
printf("%d", n);
  1. A. 255
  2. B. 256
  3. C. 300
  4. D. 1001

Correct answer: D. 1001

An unsigned char never exceeds 255; after 255, c++ wraps it to 0. c < 300 is therefore always true and only the break ends the loop, when n reaches 1001. '256' assumes the loop stops at the wrap.

More C Programming questions

Exams that ask this topic