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

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.

StringsBeginner7 min readJul 7, 2026
Analogies

1. Intro

A C-style string is a sequence of characters stored in a char array and terminated by a special null character '\0'. This is the original way text was represented in the C language and is still supported in C++ for backward compatibility and low-level work. Unlike std::string, a C-style string has a fixed size determined at declaration and must be manipulated manually using functions from <cstring>.

🏏

Cricket analogy: A printed team sheet with a fixed number of blank lines for a captain's name, terminated by a blank line marker, is like a C-style string — a fixed-size char array ending in '\0' — kept for legacy scorebooks even though modern digital scoring (std::string) is more flexible.

2. Syntax

cpp
#include <cstring>
using namespace std;

char name[] = "Alex";              // array sized automatically, includes '\0'
char buffer[20] = "Hello";         // fixed-size buffer
char empty[10] = "";               // empty C-style string
const char* literal = "World";      // pointer to a string literal

3. Explanation

When you write char name[] = "Alex";, the compiler creates an array of 5 characters: 'A', 'l', 'e', 'x', and the terminating '\0'. Functions that operate on C-style strings, such as strlen(), strcpy(), strcat(), and strcmp(), all rely on finding this null terminator to know where the string ends — if it is missing, the function keeps reading past the array bounds. Because the buffer size is fixed at declaration, C-style strings do not automatically grow; the programmer is responsible for making sure the destination array is large enough to hold the result of any copy or concatenation operation.

🏏

Cricket analogy: char name[] = "Alex"; stores 'A','l','e','x' plus a hidden '\0', 5 characters total; a scoring function like strcmp() comparing player names 'Alex' and 'Alexis' would read past a too-small buffer if the programmer forgot to size it for the terminator, corrupting the scorecard.

Buffer overflow risk: functions like strcpy() and strcat() do not check whether the destination array is big enough. Writing more characters than the array can hold overwrites adjacent memory and causes undefined behavior — a classic source of security bugs. Always size destination buffers generously or prefer std::string.

4. Example

cpp
#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char greeting[20] = "Hello";
    char name[] = "Alex";

    strcat(greeting, ", ");
    strcat(greeting, name);

    cout << greeting << endl;
    cout << "Length: " << strlen(greeting) << endl;
    return 0;
}

5. Output

text
Hello, Alex
Length: 11

6. Key Takeaways

  • A C-style string is a char array terminated by the null character '\0'.
  • Manipulation requires functions from <cstring> such as strlen(), strcpy(), strcat(), and strcmp().
  • Buffer size is fixed at declaration, so overflow can occur if the destination array is too small.
  • std::string is generally preferred for safety, but C-style strings remain common in low-level and legacy C++ code.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#CStyleStringsInC#Style#Strings#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep