Introduction / Context:
In C and C++, a string literal such as "Hello" is stored as an array of characters terminated by a special character. Recognizing this terminator is crucial when working with C-style strings, pointer arithmetic, standard library functions like strlen, and interfacing with APIs that expect null-terminated strings.
Given Data / Assumptions:
- String literals are compile-time constants of array type (e.g., const char[6] for "Hello").
- Most C library string functions rely on a sentinel terminator.
- We are not referring to std::string objects (which manage size separately).
Concept / Approach:
- The terminator is the null character, written as '\0' and having value 0.
- It marks the end of the sequence for functions that process C strings.
- It is automatically appended after the last character of a string literal.
Step-by-Step Solution:
Take "Hi": underlying storage is {'H', 'i', '\0'}.For "Hello": storage is 6 chars → 'H' 'e' 'l' 'l' 'o' '\0'.Therefore, the automatically appended character is the null character.
Verification / Alternative check:
sizeof("Hello") equals 6 (including terminator) while strlen("Hello") equals 5 (excluding terminator), confirming presence of '\0'.
Why Other Options Are Wrong:
- a space / # / *: None of these are special terminators in C/C++ strings.
- None of the above: Incorrect because the null character is the correct terminator.
Common Pitfalls:
- Forgetting the terminator when allocating buffers, causing off-by-one errors.
- Confusing std::string length with C-string length and storage.
Final Answer:
a null character
Discussion & Comments