Concatenating Strings

How to Concatenate Strings in C++

Learn how to concatenate strings in C++ with std::string using +, +=, and append(), plus numbers with to_string and C-style strcat, with examples.

On this page
  1. The short answer
  2. Adding to a string with += and append()
  3. Mixing text and numbers
  4. Joining string literals at compile time
  5. Why “Hello” + “ world” does not compile
  6. Concatenating C-style strings
  7. Joining a list of strings
  8. Common mistakes
  9. Choosing a method

The short answer

In C++, join two std::string values with the + operator:

#include <iostream>
#include <string>

int main() {
    std::string first = "Hello, ";
    std::string second = "world!";
    std::string message = first + second;

    std::cout << message << '\n';  // Hello, world!
}

The + operator builds a new string that holds the characters of both operands, in order. The original strings do not change.

To add text to a string you already have, use += or append(). The sections below cover both, along with numbers, C-style strings, and the mistakes that cause the most trouble.

Adding to a string with += and append()

+= adds text to the end of the string on its left side:

std::string message = "Hello";
message += ", ";
message += "world!";
// message is now "Hello, world!"

+= also accepts a single char, so message += '!'; adds one exclamation mark.

append() is a member function that does the same job. It returns a reference to the string, so calls can be chained:

std::string message = "Hello";
message.append(", ").append("world!");
// message is now "Hello, world!"

std::string has many other member functions. For an overview of the rest, see c string methods.

Mixing text and numbers

+ cannot add a number directly to a string literal. This line compiles, but it is wrong:

int score = 42;
std::string label = "Score: " + score;  // compiles, but this is pointer arithmetic

The literal is treated as a pointer, so adding score moves that pointer. The result is undefined behavior, not the text “Score: 42”.

Convert the number to text first with std::to_string, which is declared in <string>:

int score = 42;
std::string label = "Score: " + std::to_string(score);  // "Score: 42"

std::to_string works with integers and floating-point numbers. Its output for a double uses a fixed format with six digits after the decimal point, so std::to_string(3.5) gives "3.500000". For other ways to turn a number into text, see c convert int to string.

Joining string literals at compile time

Adjacent string literals are joined by the compiler before the program runs:

const char* text = "Hello, " "world!";  // same as "Hello, world!"

This is handy for splitting a long literal across lines. It only works for literals written next to each other, not for variables.

Why “Hello” + “ world” does not compile

Two string literals cannot be joined with +:

std::string s = "Hello, " + "world!";  // does not compile

A string literal is usually treated as a pointer to its first character, and C++ does not define + between two pointers. To fix it, make at least one side a std::string:

std::string s = std::string("Hello, ") + "world!";  // works

Concatenating C-style strings

C-style strings are char arrays that end with a null character ('\0'). The functions for joining them are in <cstring>. The most common is std::strcat:

#include <cstring>
#include <iostream>

int main() {
    char greeting[32] = "Hello, ";
    std::strcat(greeting, "world!");

    std::cout << greeting << '\n';  // Hello, world!
}

std::strcat copies the second string to the end of the first, starting at the first string’s null character. It does not check the size of the array, so the destination must have room for the whole result plus the final null character. In this example, 13 characters plus the null character fit in the 32-byte array.

strncat and snprintf

std::strncat takes a third argument, the maximum number of characters to copy from the second string. That number limits what is copied from the source. It is not the size of the destination, so passing the full array size is a common mistake. A safer call leaves room for the terminating null character:

char greeting[32] = "Hello, ";
std::strncat(greeting, "world!", sizeof(greeting) - std::strlen(greeting) - 1);

std::snprintf, from <cstdio>, is another option. It writes formatted text into a buffer and stops at the size you give it:

char buffer[32];
std::snprintf(buffer, sizeof(buffer), "%s%s", "Hello, ", "world!");
// buffer holds "Hello, world!"

If the result is too long for the buffer, snprintf truncates it. It always ends the buffer with a null character, as long as the size is at least 1.

For new code, std::string is usually the simpler choice. The C-style functions matter most when an older library or interface expects char arrays.

Joining a list of strings

To join many strings with a separator, add the separator inside a loop:

#include <cstddef>
#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> words = {"red", "green", "blue"};
    std::string joined;

    for (std::size_t i = 0; i < words.size(); ++i) {
        if (i > 0) {
            joined += ", ";
        }
        joined += words[i];
    }

    std::cout << joined << '\n';  // red, green, blue
}

If you know the final length ahead of time, calling joined.reserve(total_length) before the loop can reduce how often the string has to grow its storage.

Common mistakes

  • Adding two string literals. "Hello" + " world" does not compile. See the section above.
  • Forgetting the space. std::string("Hello") + "world" gives Helloworld. Put the space inside one of the pieces.
  • Adding a char to a literal. "Hello" + '!' compiles, but it does pointer arithmetic rather than concatenation, and the result is undefined behavior. Write std::string("Hello") + '!' instead.
  • Assuming the buffer is big enough. std::strcat does not check the size of the array. Count the characters, plus one for the null character, before you call it.
  • Writing to a string literal. The destination of std::strcat must be a writable array, such as char greeting[32]. A pointer to a string literal is not safe to write to.
  • Missing headers. std::string needs <string>, std::strcat and std::strncat need <cstring>, and std::snprintf needs <cstdio>.

Choosing a method

  • Use + and += with std::string for most code.
  • Use append() when you prefer a member function, or when you want to add several copies of one character.
  • Use std::to_string to turn numbers into text.
  • Use std::strcat, std::strncat, and std::snprintf when your code works with char arrays.
  • If your compiler supports C++20, std::format from <format> can build a string from a template with placeholders for values.

Joining is only half of the job. To check whether two strings match after you build them, read how to compare two strings in c. To go the other way and break text into parts, see c split string by delimiter.

Keep exploring

Guides from other topics

All topics →

Shift Operators in C++

Learn how the << and >> shift operators work in C++, with binary examples, compound forms, precedence traps, undefined cases, and safe ways to use them.