ProgrammingVery hardProduct company OA · GATE CS

Which statement about this C declaration is true?

Which statement about this C declaration is true?
char a[5] = "hello";
  1. A. a holds "hell" followed by '\0'
  2. B. It fails to compile because "hello" needs 6 bytes
  3. C. It compiles, but a holds no terminating '\0', so passing it to strlen is undefined
  4. D. The compiler silently enlarges a to 6 bytes

Correct answer: C. It compiles, but a holds no terminating '\0', so passing it to strlen is undefined

C has a special rule: if the array has exactly room for the characters but not the '\0', the terminator is simply dropped. a is 5 chars, not a string, so string functions would run off its end. (C++ rejects this line.)

More C Programming questions

Exams that ask this topic