Home C String Functions strncat() C strncat() Function Definition In C, strncat() adds at most n characters from the source onto the end of the destination. It always writes an extra '\0' after those characters.
You pass the destination, the source, and the count. All three are required. The destination needs room for the old text, the new characters, and that extra '\0'. Include <string.h>.
Syntax Pass destination, source, and count. Start with this call.
strncat(dest, "world", 5); Call What it does strncat(dest, "world", 5)Adds up to 5 characters from world, then writes '\0'.
Include <string.h>. n is not the size of dest. Leave room for the final '\0'.
Library declarations char *strncat(char *dest, const char *src, size_t n); Parameters Parameter Description dest Required. The C-string you add onto. It must have free space. source Required. The characters you copy from. count Required. The most characters to take from source.
Return Value Return Value Description dest pointer The same destination pointer you passed in.
C Examples 1. Append n characters 2. Cap by remaining space 3. When n exceeds src Example 1 — Append n Characters Append only the first three letters of the source onto dest.
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[16] = "Hi";
strncat(dest, "There", 3);
printf("%s\n", dest);
return 0;
} How It Works Start with "Hi". dest already has a terminator.Take 3 from src. Characters T, h, e are appended.Add '\0'. strncat always writes the terminator.Result. Printing shows HiThe.Example 2 — Cap by Remaining Space Use sizeof(dest) - strlen(dest) - 1 so the append cannot overflow the array.
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[8] = "Hi";
size_t room = sizeof dest - strlen(dest) - 1;
strncat(dest, "ABCDEF", room);
printf("%s\n", dest);
return 0;
} How It Works Measure room. Array is 8 bytes; "Hi" uses 2 + terminator → 5 free character slots.Pass room as n. At most 5 characters from "ABCDEF" are appended.Result fits. "HiABCDE" plus '\0' fills the buffer safely.Key lesson. n must reflect free space—strncat will not check sizeof for you.Example 3 — When n Exceeds src If n is larger than the source length, the whole source is appended (still with a final '\0').
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[16] = "Go";
strncat(dest, "!", 10);
printf("%s\n", dest);
return 0;
} How It Works Source is short. "!" has length 1.n = 10. Only one character exists before '\0', so only '!' is copied.Terminator added. Dest becomes "Go!".Still size dest. A large n does not create room—the buffer must already be big enough.Frequently Asked Questions What does strncat() do in C? strncat() appends at most n characters from src onto dest and always writes a final '\0'.
What are the parameters of strncat()? All three are required. dest, source, and count control the append.
What does strncat() return? The same dest pointer you passed in.
Which header file does strncat() need? Include <string.h>.
Is n the size of dest? No. n is how many characters to take from source. Leave room for an extra '\0'.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful
Helpful Share Copy link Suggestion