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
#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 literal3. 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
#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
Hello, Alex
Length: 116. Key Takeaways
- A C-style string is a
chararray terminated by the null character'\0'. - Manipulation requires functions from
<cstring>such asstrlen(),strcpy(),strcat(), andstrcmp(). - Buffer size is fixed at declaration, so overflow can occur if the destination array is too small.
std::stringis generally preferred for safety, but C-style strings remain common in low-level and legacy C++ code.
Practice what you learned
1. What character marks the end of a C-style string?
2. Which header provides functions like `strlen()` and `strcpy()`?
3. What is the array size of `char name[] = "Alex";`?
4. Why is `strcpy()` considered risky?
5. Compared to `std::string`, what is a key limitation of C-style strings?
Was this page helpful?
You May Also Like
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.
String Concatenation in C++
Learn how to join strings together in C++ using the + and += operators for std::string, and strcat() for C-style strings, along with safety concerns.
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.
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