The strncat() function appends at most n characters from one C-string to another. It is the bounded cousin of strcat(): you control how much of the source is copied, and the result is always null-terminated.
01
Bounded
Max n chars.
02
Appends
After dest end.
03
Null ends
Always '\0'.
04
Returns dest
Same pointer.
05
string.h
Standard C.
06
vs strcat
Full append.
Fundamentals
Definition and Usage
strncat stands for string n concatenate. It finds the null terminator of dest, then copies up to n characters from src, and writes a new '\0' after them.
If strlen(src) < n, the entire source string is appended. If strlen(src) ≥ n, only the first n characters are copied—the '!' in "world!" is dropped when n is 5.
💡
Beginner Tip
n limits the source, not the destination. Before calling strncat, compute free space: size_t room = sizeof(dest) - strlen(dest) - 1; then pass the smaller of n and room when building into a fixed buffer.
The buffer holds 12 bytes. After "ID:" (3 chars + null), 8 characters fit before the final '\0'. Passing cap as n prevents writing past the end of dest.
strcat always pulls the full "there!". strncat(..., 4) stops after t h e r. Both results are valid null-terminated strings.
Applications
🚀 Common Use Cases
Fixed-size buffers — append user input without exceeding array bounds.
Truncated labels — add a suffix when only a few characters may fit on screen.
Log messages — cap how much of a long error string is appended to a line.
Protocol fields — concatenate fixed-width token fragments in embedded code.
Teaching strcat safety — introduce bounded concatenation before snprintf.
🧠 How strncat() Works
1
Find dest end
Scan to the existing '\0'.
Locate
2
Copy up to n
Take at most n bytes from src (or all of src if shorter).
Append
3
Write '\0'
Always null-terminate the result in dest.
Terminate
=
📝
char* dest
Extended C-string, safely terminated.
Important
📝 Notes
n limits source characters, not destination capacity—track buffer size yourself.
dest must already be null-terminated before the call.
Unlike strncpy, strncat always adds a final '\0'.
If dest and src overlap, behavior is undefined.
Modern code often prefers snprintf(dest + len, room, "%s", src) for clearer bounds.
Performance
⚡ Optimization
strncat must scan dest to find its end on every call, so repeated appends to the same buffer cost O(n²) total. Keep a running length or use snprintf with an offset when building long strings in a loop.
Wrap Up
Conclusion
strncat() gives you controlled concatenation: append at most n characters from a source while keeping the result null-terminated. Pair it with a remaining-space calculation to avoid buffer overflows in fixed arrays.
Next, learn strncmp() to compare strings up to a length limit.
Initialize dest as a valid empty C-string when starting fresh
Include <string.h> for strlen and strncat
Prefer snprintf in new code when formatting matters
❌ Don’t
Assume n alone prevents buffer overflow
Use strncat on an uninitialized dest
Pass overlapping dest and src buffers
Call strncat in tight loops without tracking length
Confuse strncat null rules with strncpy
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about strncat()
Use these points when appending with a length limit in C.
5
Core concepts
📝01
At Most n
Limits src.
Basics
📝02
Null Term
Always ends.
Safety
🔢03
Room Calc
Dest space.
Pattern
📈04
vs strcat
Full append.
Compare
💬05
Returns dest
Same ptr.
API
❓ Frequently Asked Questions
strncat() appends at most n characters from src to the end of dest. It always writes a null terminator after the appended text. Unlike strcat(), you can cap how many bytes are copied from the source.
char* strncat(char* dest, const char* src, size_t n); Include <string.h>. dest must already be a valid null-terminated string with enough remaining space for the append plus '\0'.
Yes. strncat always adds a terminating null byte after the characters it appends. This is different from strncpy(), which may leave dest unterminated when n is smaller than the source length.
strncat copies the entire src string (up to and including its internal '\0' is not copied as part of the n count—the function adds its own terminator). In practice, if src is shorter than n, all of src is appended.
strcat() appends the whole src string with no length limit. strncat(dest, src, n) appends at most n characters from src. You still must ensure dest has room for the existing text, up to n new characters, and the final '\0'.
No. n limits only how much is taken from src—not how large dest is. You must compute remaining space yourself, for example: cap = sizeof(dest) - strlen(dest) - 1, then strncat(dest, src, cap).
Did you know?
strncat and strncpy look similar but behave differently around null termination. strncat always finishes with '\0', while strncpy may leave the destination unterminated if the source is longer than n. Beginners often mix up these two—remember: cat always completes the string.