1. Intro
String concatenation means joining two or more strings into one. C++ makes this easy for std::string objects using the + and += operators, which automatically handle memory resizing. For C-style character arrays, concatenation must be done with the strcat() function from <cstring>, which requires the programmer to manage buffer size manually.
Cricket analogy: Joining a player's first and last name with + on std::string automatically resizes to fit "Virat" + "Kohli", but a fixed-size C-style name buffer requires manually calling strcat() and carefully sizing the array beforehand.
2. Syntax
#include <string>
#include <cstring>
using namespace std;
// std::string concatenation
string a = "Hello";
string b = a + " World"; // using +
a += "!"; // using +=
// C-style string concatenation
char c[20] = "Hello";
strcat(c, " World"); // appends into c, must have room3. Explanation
For std::string, the + operator creates a brand-new string that is the combination of the operands, while += appends directly onto the existing string object; both automatically grow the underlying buffer as needed. For C-style strings, strcat(destination, source) appends the contents of source onto destination, relying on destination's existing null terminator to know where to start writing, and it writes a new null terminator at the end. Because strcat() does not check whether destination has enough remaining space, concatenating into an undersized buffer overwrites memory beyond the array.
Cricket analogy: Using + to combine "Player: " and "Rohit Sharma" creates a brand-new string, while nameLabel += " (c)" appends the captaincy tag directly onto the existing string, both auto-growing the buffer; strcat() instead relies on finding the existing null terminator in a fixed C-array before writing, and an undersized buffer would silently corrupt nearby memory.
strcat() (and strcpy()) never verify that the destination buffer is large enough to hold the result. If the combined text exceeds the destination array's capacity, the extra bytes overwrite adjacent memory — a buffer overflow. Always size the destination array with enough headroom, or use std::string concatenation instead.
4. Example
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main() {
string first = "Hello";
string second = first + ", C++!";
cout << second << endl;
char cArr[30] = "Hello";
strcat(cArr, ", C++!");
cout << cArr << endl;
return 0;
}5. Output
Hello, C++!
Hello, C++!6. Key Takeaways
std::stringsupports concatenation directly through the+and+=operators, resizing automatically.- C-style strings require
strcat()from<cstring>to concatenate. strcat()performs no bounds checking, so the destination buffer must be pre-sized large enough.- Using undersized buffers with
strcat()/strcpy()is a common source of buffer overflow bugs.
Practice what you learned
1. Which operator can be used to concatenate two `std::string` objects into a new string?
2. Which function concatenates two C-style strings?
3. What must be true about the destination array before calling `strcat()`?
4. What does `a += "!";` do when `a` is a `std::string`?
5. What risk does `strcat()` pose that `std::string` concatenation does not have?
Was this page helpful?
You May Also Like
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.
String Functions in C++
Explore the most commonly used std::string member functions — length, substr, find, append, at, empty, and c_str — with practical examples.
String Comparison in C++
Learn how to compare strings in C++ using relational operators for std::string and strcmp() for C-style strings, and avoid the strcmp() boolean trap.
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