ProgrammingHardTCS NQT · Infosys

What is printed?

What is printed?
const char *s = "a1b2c3d4";
int sum = 0;
for (; *s; s++) {
    if (*s >= 'a') continue;
    if (*s == '3') break;
    sum += *s - '0';
}
printf("%d", sum);
  1. A. 10
  2. B. 3
  3. C. 6
  4. D. 1

Correct answer: B. 3

Letters are skipped by continue (in ASCII, letters have larger codes than digits). The digits 1 and 2 are added; at '3' the loop breaks, so 3 and 4 are never added. '10' ignores the break; '6' adds the 3 before breaking.

More C Programming questions

Exams that ask this topic