1. Intro
An array of characters is one of the most common uses of arrays in C++: it is how the language represents a C-style string. A C-style string is simply a char array where a special null character \0 marks the end of the text, letting functions know where the string stops even though the underlying array may have extra unused capacity.
Cricket analogy: A scoreboard display stops printing a batsman's name at a trailing blank space rather than showing the whole fixed-width name field, similar to how a C-style string's char array uses \0 to mark where meaningful text actually ends.
2. Syntax
// Declared with an explicit null terminator
char name[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
// Declared with a string literal (null terminator added automatically)
char greeting[] = "Hello";3. Explanation
When you write char greeting[] = "Hello";, the compiler automatically stores the 5 characters H, e, l, l, o, followed by an implicit \0 terminator, making the array's actual size 6, not 5. The \0 character marks the logical end of the string — functions like cout << and the standard string-handling functions (strlen, strcpy, and similar) scan the char array until they hit \0 and stop there, ignoring any characters after it. This null-terminated char array is what is called a C-style string, and it is distinct from the C++ string class (#include <string>), which manages its own memory, can grow dynamically, and offers many convenient member functions — a C-style char array, by contrast, has a fixed size and requires manual, careful handling.
Cricket analogy: Just as scoring "Hello" reserves 6 slots (H-e-l-l-o-\0) though only 5 letters are visible, a scorecard entry for 'SACHIN' effectively reserves one extra silent slot for the terminator that strlen() uses to know where the name ends.
4. Example
#include <iostream>
using namespace std;
int main() {
char greeting[] = "Hello";
cout << "Greeting: " << greeting << endl;
cout << "Characters: ";
for (int i = 0; greeting[i] != '\0'; i++) {
cout << greeting[i] << " ";
}
cout << endl;
return 0;
}5. Output
Greeting: Hello
Characters: H e l l o 6. Key Takeaways
- A C-style string is a
chararray terminated by the null character\0. - A string literal like
"Hello"automatically adds the\0terminator, so its array size is one more than the visible characters. coutand standard string functions rely on\0to know where the string ends, ignoring anything stored after it.- A C-style char array has a fixed size and manual memory handling, unlike the more flexible, dynamically-sized C++
stringclass.
Practice what you learned
1. What character marks the end of a C-style string?
2. What is the actual array size of `char greeting[] = "Hello";`?
3. How does `cout <<` know where to stop printing a char array?
4. What is a key difference between a C-style char array string and the C++ `string` class?
5. What would happen if a char array meant to hold a C-style string was missing its '\0' terminator?
Was this page helpful?
You May Also Like
Arrays in C++
Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.
C-Style Strings in C++
Understand C-style strings as null-terminated char arrays, the standard library functions used to manipulate them, and why they are riskier than std::string.
Strings in C++
Learn how the C++ `std::string` class represents text, why it is safer and more convenient than C-style char arrays, and how to declare and use it.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics