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.
Fundamentals
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.
Foundation
📝 Syntax
The function signature from the C standard library:
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++)
Cheat Sheet
⚡ Quick Reference
Before
Call
After 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 value
same 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
Hands-On
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.
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.
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.
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.
Important
📝 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.
Performance
⚡ 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.
Wrap Up
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.
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)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strcat()
Use these points whenever you append C-style strings.
5
Core concepts
🔗01
Append In Place
Modifies destination.
Basics
📝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.