Concatenating Strings

What Is the CONCATENATE() Function Used For?

Spreadsheets like Excel and Google Sheets use CONCATENATE() to join text. C++ has no such function, but you can join strings with + or append().

On this page
  1. The short answer
  2. C++ has no CONCATENATE() function
  3. The C++ version of a spreadsheet join
  4. Joining a number
  5. When this matters

The short answer

CONCATENATE() is a spreadsheet function. It joins text from several cells or values into one piece of text. In Excel or Google Sheets, this formula returns Hello, world!:

=CONCATENATE("Hello, ", "world!")

People use it to build a label, a name, or a code from separate parts, such as a first name in one cell and a last name in another.

C++ has no CONCATENATE() function

The C++ standard library has no function named CONCATENATE(). Code that needs to join text uses other tools:

  • The + and += operators on std::string.
  • The append() member function of std::string.
  • The strcat family in <cstring>, for C-style strings.

The name strcat may sound like a spreadsheet function, but it comes from C and works differently. It does not create a new string. It changes the char array you pass in, so that array must be large enough for the result.

The C++ version of a spreadsheet join

Suppose two cells hold a first name and a last name. In a spreadsheet, you might write:

=CONCATENATE(A1, " ", B1)

In C++, the same join uses std::string values:

#include <iostream>
#include <string>

int main() {
    std::string first = "Ada";
    std::string last = "Lovelace";
    std::string full_name = first + " " + last;

    std::cout << full_name << '\n';  // Ada Lovelace
}

Each + joins the text so far with the next piece, from left to right. The space is its own piece, so the code adds it explicitly.

Joining a number

Spreadsheets usually convert numbers to text for you. In C++, convert a number with std::to_string before you join it:

int count = 3;
std::string label = std::to_string(count) + " items";  // "3 items"

For more ways to convert numbers, see c convert int to string.

When this matters

If you learned to join text in a spreadsheet, the idea carries over to C++. You still combine pieces of text. In code, you write each join explicitly, which makes the result easy to check with a print statement or a test.

For the general idea behind joining text, see what is string concatenation. For the full set of options in C++, see how to concatenate strings in c.

The complete guide to concatenating strings

How to Concatenate Strings in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

How to Use cout in C++

Learn how to use cout in C++ to print text, numbers and variables, add new lines, and format output, with short complete examples you can compile and run.

cout 4 min read