ProgrammingHardProduct company OA

What is printed?

What is printed?
const char *s = "a1b22c333";
int sum = 0, cur = 0;
for (; *s; s++) {
    if (*s >= '0' && *s <= '9') cur = cur * 10 + (*s - '0');
    else { sum += cur; cur = 0; }
}
sum += cur;
printf("%d", sum);
  1. A. 14
  2. B. 333
  3. C. 356
  4. D. 23

Correct answer: C. 356

Digits accumulate into a number until a letter ends it: 1, 22 and 333. The last number is added by the line after the loop, so the sum is 356. '14' adds digits one by one; '23' forgets the final sum += cur.

More C Programming questions

Exams that ask this topic