The strncat() function appends up to num characters from a source C-string onto the end of a destination C-string, then adds a null terminator. It is the bounded version of strcat()—you choose how much of the source to copy when you do not want the entire string appended.
01
Append n Chars
Limit from source.
02
Null Terminate
Always adds \0.
03
Return dest
Pointer returned.
04
Buffer Size
Still your job.
05
vs strcat()
Full vs partial.
06
<cstring>
C library function.
Fundamentals
Definition and Usage
In C++, strncat() is declared in <cstring>. It finds the existing null terminator of destination, then copies characters from source starting there—at most num characters, or until source ends if it is shorter than num.
After copying, strncat always writes a final '\0'. The destination must already contain a valid C-string (often initialized with text or "") and must have enough unused bytes for the append plus terminator.
💡
Beginner Tip
num limits source characters only. It does not mean “total buffer size.” Ensure sizeof(dest) > strlen(dest) + num (with room for \0) before calling strncat.
strncat starts at the null byte after "Hello, ", copies six characters World!, then writes '\0'. All six source characters fit within the limit num = 6.
Example 2 — Append Only Three Characters
Limit the append when you need a prefix of the source, not the full word.
Using strlen(part) as num appends each segment fully while keeping an explicit bound. In new C++ code, std::filesystem::path is safer; this pattern shows how C-style APIs compose strings.
Applications
🚀 Common Use Cases
Truncated concatenation — append only a prefix of untrusted or long input.
Fixed-width fields — combine labels with limited suffix text.
Legacy C APIs — build messages in char buffers passed to older libraries.
Incremental path assembly — join path segments with explicit limits.
Teaching bounded I/O — step between unrestricted strcat and safer modern types.
🧠 How strncat() Works
1
Find end of dest
Scan destination until the existing '\0'.
Locate
2
Copy up to num
Copy characters from source, stopping at num or source '\0'.
Copy
3
Write terminator
Place '\0' after the last copied character.
Terminate
✓
➕
Return destination
Destination now holds the original text plus the bounded append.
Important
📝 Notes
destination must already be null-terminated before the call.
num caps source length only—you must still size the destination buffer correctly.
strncat always null-terminates (unlike strncpy in edge cases).
Source and destination must not overlap (same rule as strcat).
Prefer std::string or std::string::append in new C++ application code.
Performance
⚡ Optimization
strncat() scans the destination to find its end on every call, so repeated appends to the same buffer cost O(n) per call. For many concatenations, track the write position or use std::string, which avoids rescanning from the start each time.
Wrap Up
Conclusion
strncat() gives controlled appends from a source C-string while always keeping the result null-terminated. Use it when you need at most num characters from source, but still verify destination capacity yourself.
Next, learn strncmp() to compare at most n characters of two C-strings—the bounded counterpart to strcmp().
Initialize destination as "" or with existing text
Verify buffer space: strlen(dest) + num + 1 <= capacity
Use strlen(source) as num to append a full segment safely
Prefer std::string for dynamic concatenation
Include <cstring> and use std::strncat
❌ Don’t
Assume num alone prevents buffer overflow
Call on an uninitialized destination array
Overlap source and destination memory
Use strncat when you mean strncpy (replace vs append)
Append unbounded user input without validating total length
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strncat()
Use these points when appending to C-string buffers.
5
Core concepts
➕01
Append n
Limit source.
Basics
🔢02
Always \0
Terminated.
Safety
🛡03
Buffer Size
Your duty.
Rule
🔄04
vs strcat
Partial vs full.
Compare
📝05
Return dest
Same pointer.
Return
❓ Frequently Asked Questions
strncat() appends at most num characters from source to the end of destination, then writes a null terminator. Unlike strcat(), it limits how many source characters are copied—useful when source might be longer than you want to append.
char* strncat(char* destination, const char* source, size_t num); Include <cstring>. destination must already be a null-terminated string with enough remaining space for the appended characters plus '\0'.
It returns destination—the same pointer passed as the first argument. The destination buffer holds the combined string after the call.
Yes. strncat() always adds a '\0' after the appended characters. This differs from strncpy(), which may leave destination unterminated when num is smaller than the source length.
Not automatically. num only caps how many characters are taken from source—it does not check whether destination has enough total space. You must ensure the buffer can hold strlen(dest) + (characters appended) + 1 bytes.
strcat() appends the entire source string. strncat() appends at most num characters from source (or until source ends if shorter). Both require a large enough destination buffer; strncat() gives finer control over how much of source is copied.
Did you know?
The “n” in strncat limits characters copied from source, not the total size of the destination array. Decades of bugs came from assuming strncat was fully “safe” without checking how big destination actually is.