ProgrammingHard

char a[3] = {'x', 'y', 'z'}; Which call reads a correctly with no undefined behaviour?

  1. A. printf("%.3s", a);
  2. B. printf("%s", a);
  3. C. strlen(a);
  4. D. strcpy(b, a); where char b[3]

Correct answer: A. printf("%.3s", a);

a has no terminator, so any call that searches for '\0' (%s, strlen, strcpy) reads past the array. A precision of 3 stops exactly at the end of a.

More C Programming questions

Exams that ask this topic