ProgrammingModerateTCS NQT · Wipro

What is printed?

What is printed?
char s[8] = {'a', 'b'};
printf("%zu %zu %d", strlen(s), sizeof s, s[7]);
  1. A. 2 8 0
  2. B. 2 2 0
  3. C. 2 8 garbage
  4. D. 8 8 0

Correct answer: A. 2 8 0

When an array has an initialiser, the elements not listed are set to 0, so s[2] to s[7] are '\0'. That makes s a valid string of length 2, sizeof s is the declared 8, and s[7] is 0.

More C Programming questions

Exams that ask this topic