ProgrammingHardProduct company OA

What is printed?

What is printed?
const char *s = "abcdefgh";
const char *p = s + 2, *q = strchr(s, 'g');
printf("%td %c %s", q - p, *(p + (q - p) / 2), q);
  1. A. 4 e g
  2. B. 4 e gh
  3. C. 5 e gh
  4. D. 4 d gh

Correct answer: B. 4 e gh

p points at 'c' (index 2) and q at 'g' (index 6), so q − p = 4. The midpoint is p + 2, index 4, 'e'. Printing q with %s prints from 'g' to the end: "gh".

More C Programming questions

Exams that ask this topic