ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
char s[] = "abcdef";
char *p = s + 2;
p[1] = 'X';
printf("%s %s %c %zu", s, p, *(p - 2), strlen(p + 4));
  1. A. abcXef cXef a 0
  2. B. abXdef Xdef a 0
  3. C. abcXef cXef c 0
  4. D. abcXef cXef a 2

Correct answer: A. abcXef cXef a 0

p points at s[2] ('c'), so p[1] is s[3]: the array becomes "abcXef" and printing from p gives "cXef". *(p - 2) is s[0] = 'a'. p + 4 is s + 6, the terminating '\0', so strlen is 0. Writing through p is fine because s is a modifiable array, not a string literal.

More C Programming questions

Exams that ask this topic