ProgrammingModerateAccenture · Capgemini pattern

What is printed?

What is printed?
char *s = "programming";
int c = 0;
while (*s)
    if (*s++ == 'm') c++;
printf("%d %c", c, *(s - 1));
  1. A. 1 g
  2. B. 2 m
  3. C. 2 g
  4. D. 3 g

Correct answer: C. 2 g

*s++ reads the current character and then advances s, so every character is compared once; "programming" has two 'm's. The loop stops when s points at the terminating '\0', so s − 1 points at the last letter, 'g'. Only s itself moves; the literal is never modified.

More C Programming questions

Exams that ask this topic