C++ String strcat() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<cstring>

What You’ll Learn

The strcat() function from <cstring> appends one C-style string to another. It is a classic C library tool still used in C++ when working with char arrays and legacy APIs. This guide covers syntax, buffer safety, return values, and when to prefer std::string instead.

01

C-String Append

Join char arrays.

02

Two Parameters

dest + source.

03

Modifies dest

In-place change.

04

Buffer Size

Must fit both strings.

05

Returns char*

Pointer to dest.

06

vs std::string

Modern alternative.

Definition and Usage

In C++, strcat() is declared in <cstring> (the C++ header for C string functions). It copies characters from source onto the end of destination, overwriting the null terminator of the destination and adding a new null terminator after the appended text.

Unlike std::string, which grows automatically, strcat() assumes you have already allocated a char array large enough to hold the combined result. The destination must contain a valid C-string before the call.

💡
Beginner Tip

Before calling strcat(dest, src), ask: “How many characters are already in dest, how long is src, and is my array big enough for both plus \0?” If unsure, use std::string.

📝 Syntax

The function signature from the C standard library:

C++
char* strcat(char* destination, const char* source);

Parameters

  • destination — pointer to a modifiable char array that already holds a null-terminated string. Must have enough remaining space for source and the final '\0'.
  • source — pointer to the C-string to append (read-only). Cannot overlap with destination in a way that violates the standard; do not append a string to itself.

Return Value

Returns destination (the same pointer passed in). The concatenated text is stored in the destination array.

Header

#include <cstring>   // C++ style
// or
#include <string.h>  // C style (also works in C++)

⚡ Quick Reference

BeforeCallAfter dest
dest = "Hello, "strcat(dest, "World!")"Hello, World!"
dest = "C++"strcat(dest, " is fun")"C++ is fun"
dest = ""strcat(dest, "Start")"Start"
dest = "Hi"return valuesame as dest pointer
Basic
strcat(dest, "text");

Append to dest

Size check
strlen(dest)+strlen(src)+1

Min bytes needed

Init buffer
char buf[64] = "";

Empty C-string

Modern C++
s += "text";

std::string append

Examples Gallery

Compile with g++ strcat.cpp -std=c++17 -o strcat. Every example uses a buffer large enough for the result—always plan capacity before calling strcat().

📚 Getting Started

Append one string to another in a fixed char array.

Example 1 — Basic strcat() Usage

Concatenate "World!" onto "Hello, " inside a buffer sized for the full result.

C++
#include <iostream>
#include <cstring>

int main() {
    char destination[20] = "Hello, ";
    const char* source = "World!";

    strcat(destination, source);

    std::cout << "Concatenated: " << destination << std::endl;
    return 0;
}

How It Works

strcat finds the '\0' at the end of "Hello, ", copies "World!" starting there, and writes a new terminator. The array must hold at least 13 characters plus null (20 is safe).

Example 2 — Build a Message in Steps

Call strcat() multiple times to assemble a sentence from pieces.

C++
#include <iostream>
#include <cstring>

int main() {
    char message[48] = "C++";
    strcat(message, " strings");
    strcat(message, " are");
    strcat(message, " powerful!");

    std::cout << message << std::endl;
    return 0;
}

How It Works

Each strcat appends to whatever is already in message. After every call, recalculate remaining space if you are near the buffer limit.

📈 Practical Patterns

Return value, capacity checks, and modern alternatives.

Example 3 — Using the Return Value

strcat() returns the destination pointer, which you can pass directly to functions expecting char*.

C++
#include <iostream>
#include <cstring>

int main() {
    char path[32] = "/cpp/string/function/";
    const char* page = "strcat";

    std::cout << "Full path: "
              << strcat(path, page) << std::endl;

    return 0;
}

How It Works

The return value equals path. Chaining works because the same buffer is modified in place and the pointer is returned for convenience.

Example 4 — Plan Buffer Size with strlen()

Calculate required space before concatenating—good habit even in beginner programs.

C++
#include <iostream>
#include <cstring>

int main() {
    const char* first = "Code";
    const char* second = "ToFun";
    char buffer[16];

    strcpy(buffer, first);
    strcat(buffer, second);

    std::size_t needed = std::strlen(first) + std::strlen(second) + 1;
    std::cout << "Result: " << buffer << std::endl;
    std::cout << "Bytes needed: " << needed << std::endl;

    return 0;
}

How It Works

strlen counts characters before the null terminator. Add lengths plus 1 for '\0'. Here 4 + 5 + 1 = 9 fits in buffer[16]. Always initialize with strcpy (or buffer[0] = '\0') before the first strcat if starting empty.

Example 5 — Same Task with std::string

Modern C++ code usually avoids manual buffers; compare the safer style side by side.

C++
#include <iostream>
#include <string>

int main() {
    std::string greeting = "Hello, ";
    greeting += "World!";

    std::string path = "/cpp/string/function/";
    path.append("strcat");

    std::cout << greeting << std::endl;
    std::cout << path << std::endl;

    return 0;
}

How It Works

std::string grows as needed—no fixed size to calculate. Learn strcat() for C strings and interviews, but prefer this style in new C++ projects.

🚀 Common Use Cases

  • Legacy C APIs — build paths or messages in char buffers required by older libraries.
  • Embedded systems — fixed-size buffers where dynamic allocation is avoided.
  • File path assembly — join directory and file name segments (when size is known).
  • Learning C foundations — understand how C-strings work before using std::string.
  • Interview basics — explain concatenation, null terminators, and buffer overflow risks.

🧠 How strcat() Works

1

Find end of destination

The function scans destination until it reaches the null terminator '\\0'.

Locate
2

Copy source characters

Each character from source is written starting at that position, including the final '\\0'.

Append
3

Destination is modified

The original array now holds the combined C-string. No second copy is allocated.

In-place
=

Return destination

The same char* you passed in, now pointing to the longer null-terminated string.

📝 Notes

  • Buffer overflow is the main danger—the function never checks array bounds.
  • Destination must be mutable and already null-terminated before the call.
  • Do not use strcat(dest, dest) or overlapping buffers unless using the safe variant from your platform docs.
  • Include <cstring> for strcat, strlen, and strcpy.
  • In C++, std::string plus += or append() is usually safer for application code.

⚡ Optimization

strcat() is efficient for small, fixed buffers because it avoids heap allocation. Repeated concatenation into the same small buffer in a loop can become slow (each call rescans from the start of the destination). For many append operations, std::string with reserved capacity (reserve()) or a single pre-sized buffer often performs better and is easier to reason about.

Conclusion

strcat() appends C-strings in place and returns the destination pointer. Master it together with strlen() and buffer sizing so you understand null terminators and overflow risks—then reach for std::string when writing everyday C++.

Practice the examples with deliberately sized buffers, then continue to strchr() for character search in C-strings.

💡 Best Practices

✅ Do

  • Size buffers using strlen(dest) + strlen(src) + 1
  • Initialize destination as a valid C-string ("" or strcpy)
  • Include <cstring> and use const char* for read-only sources
  • Prefer std::string in new C++ application code
  • Consider strncat() when limiting copied length helps

❌ Don’t

  • Append into a buffer that is too small
  • Pass uninitialized char arrays as destination
  • Assume strcat creates a new string object
  • Concatenate into overlapping memory regions
  • Use strcat on std::string::c_str() results (immutable buffer)

Key Takeaways

Knowledge Unlocked

Five things to remember about strcat()

Use these points whenever you append C-style strings.

5
Core concepts
📝 02

Two Arguments

dest and source.

Syntax
03

Check Buffer Size

Avoid overflow.

Safety
🔄 04

Returns dest

Same char* pointer.

Return
💬 05

Prefer std::string

Modern C++ style.

Alternative

❓ Frequently Asked Questions

strcat() appends the source C-string to the end of the destination C-string. Both are null-terminated char arrays (or char*). The function writes the copied characters—including a final null terminator—into the destination buffer and returns a pointer to destination.
char* strcat(char* destination, const char* source); Include <cstring> (or <string.h> in C). The destination must already contain a valid null-terminated string, and must have enough free space after that string to hold the source plus one null byte.
It returns the same pointer you passed as destination. The useful result is the modified text stored in the destination array—not a new string object.
strcat() does not check how large the destination array is. If the combined length of destination + source exceeds the allocated space, you get a buffer overflow—undefined behavior that can crash the program or create security holes. Always ensure the buffer is big enough.
For new C++ code, prefer std::string and operator+ or append()—they manage memory automatically. Use strcat() when working with C APIs, legacy code, or char buffers where you control the size explicitly.
strcat() appends the entire source string. strncat() appends at most n characters from source, giving slightly more control. Both require adequate space in the destination; strncat() still needs a properly sized buffer but limits how many characters are copied.
Did you know?

Calling strcat() on the pointer returned by std::string::c_str() is undefined behavior because that memory is owned by the string object and is not guaranteed to be writable or large enough to grow. Copy into your own char buffer first, or use std::string operations instead.

Continue the String Functions Series

Learn C-style concatenation with strcat(), then move on to strchr() for character search.

Next: strchr() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful