C++ strncat() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

Definition

In C++, strncat() adds text onto the end of a C-string, but it copies no more than count characters from the source. It always writes a '\0' after the characters it copies, so it may write one more byte than count.

All three arguments are required. dest must already have room for the old text, the new characters, and that extra '\0'. The two strings must not overlap. Include <cstring>.

Syntax

The third argument limits how much of the second string is added.

C++
strncat(dest, "World", 3);
CallWhat it does
strncat(dest, "World", 3)Adds at most 3 characters from World onto the end of dest.
Library declarations
C++
char* strncat(char* destination, const char* source, size_t count);

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
destThe same pointer you passed in, now holding the longer string.

C++ Examples

Example 1 — Basic strncat()

Append a short piece after "Hello ".

C++
#include <iostream>
#include <cstring>

int main() {
    char dest[50] = "Hello ";
    const char* src = "World!";

    strncat(dest, src, 5);

    std::cout << dest << '\n';
    return 0;
}

How It Works

  1. count is 5, so only W o r l d is copied.
  2. The '!' is not appended.
  3. A '\0' is written automatically at the end.

Example 2 — Take Only the First Few Letters

Build a short greeting from a longer source.

C++
#include <iostream>
#include <cstring>

int main() {
    char message[40] = "Hi, ";

    strncat(message, "C++ Tutorial!", 3);

    std::cout << message << '\n';
    return 0;
}

How It Works

  1. Only the first 3 characters of the source are used.
  2. Result becomes "Hi, C++".
  3. Useful when you want a prefix, not the whole string.

Example 3 — Clearer Style with std::string

Append a limited substring without managing a buffer.

C++
#include <iostream>
#include <string>

int main() {
    std::string text = "Hi, ";
    std::string src = "C++ Tutorial!";

    text.append(src, 0, 3);

    std::cout << text << '\n';
    return 0;
}

How It Works

  1. append(src, 0, 3) takes 3 characters from index 0.
  2. No manual '\0' or fixed array size.
  3. Learn strncat for C APIs; prefer std::string in new C++.

Frequently Asked Questions

strncat() adds at most count characters from one C-string onto the end of another, then writes a null character.
dest is the string you add onto. source is the text you copy from. count is the maximum number of characters to copy.
The same pointer as dest.
Yes. It always writes one after the copied characters, so dest needs one extra byte.
strcat() copies the whole source. strncat() copies at most count characters.

Next: strncmp()

Compare two C-strings using at most n characters.

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.

8 people found this page helpful