ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    char s[] = "abcdef";
    char *p = s;
    int n = 0;
    while (*p && p - s < 4)
        n += *p++ - 'a';
    printf("%d %c", n, *p);
}
  1. A. 10 e
  2. B. 6 d
  3. C. 10 f
  4. D. 6 e

Correct answer: D. 6 e

The loop adds the offsets 0, 1, 2 and 3 from 'a' and stops when p - s reaches 4, leaving p on 'e'. The sum is 6, and the trap value 10 comes from adding one extra element.

More C Programming questions

Exams that ask this topic