The strcat() function appends one C-string to another. It finds the null terminator of the destination, copies the source after it, and writes a new '\0'. It is one of the most common ways to build longer strings from smaller pieces.
01
Append
Add src to dest.
02
Modifies dest
Result in place.
03
Returns dest
Same pointer.
04
Needs space
Buffer must fit.
05
string.h
Standard C.
06
vs strncat
Safer limit.
Fundamentals
Definition and Usage
strcat stands for string concatenate. The destination dest must already contain a valid null-terminated string—often initialized as an empty string with char buf[64] = ""; or char buf[64] = {0};.
The function walks dest until it finds '\0', then copies every character from src including the final null terminator. There is no length check: you are responsible for making sure the buffer is large enough.
💡
Beginner Tip
Before calling strcat(dest, src), ask: “Does dest have room for strlen(dest) + strlen(src) + 1 bytes?” If not, use a larger buffer or a bounded function like strncat or snprintf.
Foundation
📝 Syntax
Standard C declaration:
C
char *strcat(char *dest, const char *src);
Parameters
dest — modifiable buffer that already holds a C-string.
src — string to append (not modified).
Return Value
Returns dest (pointer to the destination buffer).
The concatenated string is stored in dest; no new string is allocated.
Header
#include <string.h>
Cheat Sheet
⚡ Quick Reference
dest (before)
src
dest (after)
"Hello, "
"World!"
"Hello, World!"
"" (empty)
"C"
"C"
"/home/user"
"/docs"
"/home/user/docs"
required size
any
strlen(dest) + strlen(src) + 1 bytes
Append
strcat(dest, src);
In-place
Empty start
char buf[32] = "";
Valid dest
Size check
len + slen + 1 <= cap
Stay safe
Bounded
strncat(dest, src, n)
Limit chars
Hands-On
Examples Gallery
Compile with gcc strcat.c -std=c11 -o strcat. Every example uses a char array large enough for the result.
📚 Getting Started
Append one string to another in a fixed-size buffer.
strcat locates the '\0' after "Hello, ", copies "World!" starting there, and terminates the result. The array must hold at least 14 characters including the null byte.
Example 2 — Start From an Empty String
Build a message from scratch by appending words one at a time.
Real programs often assemble paths or URLs from parts. Always size the buffer for the worst-case combined length. For production code, snprintf can be easier to audit than multiple strcat calls.
Example 4 — Chained strcat() Calls
Nest calls using the fact that strcat returns dest.
Each inner call returns buf, so the next append continues from the updated string. This works but is hard to read—separate statements or snprintf are usually clearer.
strcat returns dest, so you can use it wherever a char* is expected. Remember that line is modified before printf runs.
Applications
🚀 Common Use Cases
Greeting messages — combine a prefix with a user name.
File paths — join directory and file name segments.
Log lines — append timestamps, levels, and text into one buffer.
CSV or text output — add commas and fields to a row buffer.
Protocol strings — build simple command lines in embedded systems.
🧠 How strcat() Works
1
Find end of dest
Scan dest until '\0' is found.
Locate
2
Copy src byte by byte
Write each character from src starting at that position.
Copy
3
Write final \0
The last copied byte from src is the null terminator.
Terminate
=
🔗
char* dest
Return dest pointing at the longer combined string.
Important
📝 Notes
dest must be writable—not a string literal like "hello".
Leave room for strlen(dest) + strlen(src) + 1 bytes.
dest and src must not overlap.
No bounds checking—overflow corrupts memory and is a common bug.
Prefer strncat, snprintf, or dynamic allocation for unknown lengths.
Performance
⚡ Optimization
Library strcat implementations scan dest from the start every time, so repeated appends to the same buffer cost O(n²) total if you keep re-walking the growing prefix. Track the write position or use stpcpy/snprintf when building long strings in a loop.
Wrap Up
Conclusion
strcat() is the standard way to append one C-string to another in place. Initialize dest correctly, size your buffer, and consider safer alternatives when input length is unpredictable.
Next, learn strncat() to limit how many characters get appended.
Forget the final null terminator requirement on dest
Chain many strcat calls in performance-critical loops without tracking length
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strcat()
Use these points when joining strings in C.
5
Core concepts
🔗01
Append
src after dest.
Basics
📝02
In Place
Modifies dest.
Storage
🔢03
Size Matters
No bounds check.
Safety
📈04
Returns dest
Chainable.
Return
💬05
vs strncat
Limit length.
Compare
❓ Frequently Asked Questions
strcat() appends the source string src to the end of the destination string dest. It finds the null terminator of dest, copies src starting there, and adds a new null terminator. The result is stored in dest itself.
char* strcat(char* dest, const char* src); Include <string.h>. dest must be a modifiable char array (or heap buffer) that is already a valid C-string with a null terminator.
It returns dest—the same pointer you passed as the first argument. This allows chaining like strcat(strcat(buf, "a"), "b"), though building strings step by step with snprintf is often clearer.
No. strcat() does not know how large dest is. You must ensure the buffer has enough free space for the existing text plus src plus the final '\0'. Writing past the end causes buffer overflow.
strcat() appends the entire src string. strncat(dest, src, n) appends at most n characters from src and always null-terminates the result (when there is room). strncat is safer when you track remaining buffer space.
No. If dest and src overlap, behavior is undefined. Use memmove-style logic or separate buffers when strings might share memory.
Did you know?
The name strcat comes from string concatenate. C89 standardized it alongside strcpy and strlen. Modern style guides often recommend snprintf for formatting because one call can express both copy and length limits clearly.