C strncat() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

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.

C
strncat(dest, "world", 5);
CallWhat 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
C
char *strncat(char *dest, const char *src, size_t n);

Parameters

ParameterDescription
destRequired. The C-string you add onto. It must have free space.
sourceRequired. The characters you copy from.
countRequired. The most characters to take from source.

Return Value

Return ValueDescription
dest pointerThe same destination pointer you passed in.

C Examples

Example 1 — Append n Characters

Append only the first three letters of the source onto dest.

C
#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

  1. Start with "Hi". dest already has a terminator.
  2. Take 3 from src. Characters T, h, e are appended.
  3. Add '\0'. strncat always writes the terminator.
  4. Result. Printing shows HiThe.

Example 2 — Cap by Remaining Space

Use sizeof(dest) - strlen(dest) - 1 so the append cannot overflow the array.

C
#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

  1. Measure room. Array is 8 bytes; "Hi" uses 2 + terminator → 5 free character slots.
  2. Pass room as n. At most 5 characters from "ABCDEF" are appended.
  3. Result fits. "HiABCDE" plus '\0' fills the buffer safely.
  4. 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').

C
#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

  1. Source is short. "!" has length 1.
  2. n = 10. Only one character exists before '\0', so only '!' is copied.
  3. Terminator added. Dest becomes "Go!".
  4. Still size dest. A large n does not create room—the buffer must already be big enough.

Frequently Asked Questions

strncat() appends at most n characters from src onto dest and always writes a final '\0'.
All three are required. dest, source, and count control the append.
The same dest pointer you passed in.
Include <string.h>.
No. n is how many characters to take from source. Leave room for an extra '\0'.

Next: strncmp()

Compare at most n characters of two C-strings.

strncmp() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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