100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Array of Characters in C++

Learn how a C-style string is a null-terminated char array in C++, how to declare and print one, and how it differs from the string class.

ArraysBeginner6 min readJul 7, 2026
Analogies

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

cpp
// 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

cpp
#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

text
Greeting: Hello
Characters: H e l l o 

6. Key Takeaways

  • A C-style string is a char array terminated by the null character \0.
  • A string literal like "Hello" automatically adds the \0 terminator, so its array size is one more than the visible characters.
  • cout and standard string functions rely on \0 to 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++ string class.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ArrayOfCharactersInC#Array#Characters#Syntax#Explanation#DataStructures#StudyNotes#SkillVeris