Home C++ Strings Functions strcat() C++ strcat() Function Definition In C++, strcat() joins two C-strings. It copies the second string onto the end of the first, starting where the old '\0' was, then writes a new '\0'.
Both strings are required. The first array must already have free space for the added characters and that new '\0'. strcat() does not grow the array, and the two strings must not overlap. Include <cstring>.
Syntax The first string is the one that grows. The second string is copied onto its end.
Call What it does strcat(dest, "C++")Adds C++ onto the end of dest.
Library declarations char* strcat(char* destination, const char* source); Parameters Parameter Description dest Required. The C-string you add onto. It must already have room for the new characters. source Required. The C-string you copy from. It is not changed.
Return Value Return Value Description dest The same pointer you passed in. It now holds both strings.
C++ Examples 1. Basic strcat 2. Append twice 3. vs std::string Example 1 — Basic strcat() Append "World!" onto "Hello ".
#include <iostream>
#include <cstring>
int main() {
char dest[50] = "Hello ";
const char* src = "World!";
strcat(dest, src);
std::cout << dest << '\n';
return 0;
} How It Works dest starts as "Hello " (with room left in the array).strcat finds the end of dest and copies src there.Result: "Hello World!" ending with '\0'. Example 2 — Append More Than Once Call strcat again to keep building one buffer.
#include <iostream>
#include <cstring>
int main() {
char message[80] = "C++";
strcat(message, " ");
strcat(message, "Tutorial");
std::cout << message << '\n';
return 0;
} How It Works First call appends a space. Second call appends "Tutorial". Keep the array big enough for every append. Example 3 — Safer Style with std::string Same idea without managing a fixed buffer yourself.
#include <iostream>
#include <string>
int main() {
std::string text = "Hello ";
text += "World!";
std::cout << text << '\n';
return 0;
} How It Works std::string grows as needed.+= appends like strcat, but safer for beginners.Learn strcat for C-style APIs; prefer std::string in new C++. Frequently Asked Questions What does strcat() do in C++? strcat() adds one C-string onto the end of another and writes a new null character at the end.
What are the parameters of strcat()? dest is the string you add onto. source is the string you copy from. source is not changed.
What does strcat() return? The same pointer as dest, which now holds both strings.
How big does the destination need to be? Big enough for the old text, the new text, and one ending null character. strcat() does not grow the array.
Can the two strings overlap? No. Overlapping source and destination is not allowed.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion