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

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.

StringsBeginner6 min readJul 7, 2026
Analogies

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

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

3. 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

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

text
Hello, C++!
Hello, C++!

6. Key Takeaways

  • std::string supports 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#StringConcatenationInC#String#Concatenation#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep