C String strncat() Function

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
<string.h>

What You’ll Learn

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.

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.

📝 Syntax

Standard C declaration:

C
char *strncat(char *dest, const char *src, size_t n);

Parameters

  • dest — modifiable buffer that already holds a null-terminated string.
  • src — string to append from (not modified).
  • n — maximum number of characters to copy from src (excluding the null byte strncat adds).

Return Value

  • Returns dest (pointer to the destination buffer).

Header

  • #include <string.h>

⚡ Quick Reference

dest (before)srcndest (after)
"Hello, ""world!"5"Hello, world"
"Hello, ""world!"10"Hello, world!"
"A""BC"0"A" (unchanged)
required spaceanyanystrlen(dest) + min(n, strlen(src)) + 1 bytes
Bounded
strncat(dest, src, n);

At most n

Full src
strncat(d, s, strlen(s))

Like strcat

Room left
cap = sz - len - 1

Dest space

Unbounded
strcat(dest, src)

All of src

Examples Gallery

Compile with gcc strncat.c -std=c11 -o strncat. Each example uses a buffer sized for the final string.

📚 Getting Started

Append only part of a source string to a destination buffer.

Example 1 — Append at Most 5 Characters

From "world!", copy only world onto "Hello, ".

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char dest[20] = "Hello, ";
    const char *src = "world!";

    strncat(dest, src, 5);

    printf("Concatenated string: %s\n", dest);

    return 0;
}

How It Works

strncat skips to the '\0' in dest, copies five bytes (w o r l d) from src, then writes '\0'. The exclamation mark is never copied because n is 5.

Example 2 — Append the Entire Source

When n is large enough, the full source string is appended.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char dest[32] = "Code";
    const char *src = "ToFun";

    strncat(dest, src, 10);

    printf("%s\n", dest);

    return 0;
}

How It Works

strlen("ToFun") is 5, which is less than n = 10, so all five characters are appended. This behaves like strcat(dest, src) in this case.

📈 Practical Patterns

Compute remaining space, handle edge cases, and compare with strcat.

Example 3 — Safe Append Using Remaining Space

Limit both the source count and the room left in the destination.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char dest[12] = "ID:";
    const char *src = "1234567890";
    size_t cap = sizeof(dest) - strlen(dest) - 1;

    strncat(dest, src, cap);

    printf("%s (len=%zu)\n", dest, strlen(dest));

    return 0;
}

How It Works

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.

Example 4 — When n Is Zero

With n = 0, nothing is appended from the source.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char dest[16] = "unchanged";
    const char *src = "EXTRA";

    strncat(dest, src, 0);

    printf("%s\n", dest);

    return 0;
}

How It Works

A zero limit copies no characters from src. dest stays exactly as it was. The function still returns dest.

Example 5 — strncat() vs strcat()

See how a character limit changes the result.

C
#include <stdio.h>
#include <string.h>

int main(void) {
    char a[20] = "Hi ";
    char b[20] = "Hi ";
    const char *tail = "there!";

    strcat(a, tail);
    strncat(b, tail, 4);

    printf("strcat:   [%s]\n", a);
    printf("strncat:  [%s]\n", b);

    return 0;
}

How It Works

strcat always pulls the full "there!". strncat(..., 4) stops after t h e r. Both results are valid null-terminated strings.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Compute room = sizeof(dest) - strlen(dest) - 1
  • Pass min(n, room) when space is limited
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about strncat()

Use these points when appending with a length limit in C.

5
Core concepts
📝 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.

Continue the String Functions Series

Master bounded concatenation with strncat(), then move on to limited comparison with strncmp().

Next: strncmp() →

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.

6 people found this page helpful