C++ strncpy() Function

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

Definition

In C++, strncpy() copies characters from one C-string into another, but it never writes more than count characters. If the source is shorter than count, the leftover bytes are filled with '\0'.

If the source is longer than count, dest does not get an ending '\0'. Add one yourself before you print it. All three arguments are required, and the two arrays must not overlap. Include <cstring>.

Syntax

Use the third argument to limit how many characters are copied.

C++
strncpy(dest, "Hello", 5);
CallWhat it does
strncpy(dest, "Hello", 5)Copies at most 5 characters from Hello into dest.

If the source is shorter than count, the rest of those bytes are filled with '\0'.

Library declarations
C++
char* strncpy(char* destination, const char* source, size_t count);

Parameters

ParameterDescription
destRequired. The array you copy into. It must hold at least count characters.
sourceRequired. The characters you copy from.
countRequired. The most characters to write.

Return Value

Return ValueDescription
destThe same pointer you passed in.

C++ Examples

Example 1 — Basic strncpy()

Copy a short greeting into a larger buffer.

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

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

    std::strncpy(dest, src, sizeof dest);

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

How It Works

  1. Source is shorter than count.
  2. '\0' from source is copied, then extra zeros pad to count.
  3. Printing dest is safe because it is terminated.

Example 2 — Short Source Pads with Zeros

See that leftover slots become '\0'.

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

int main() {
    char dest[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
    const char* src = "hi";

    std::strncpy(dest, src, 5);

    for (int i = 0; i < 6; ++i) {
        if (dest[i] == '\0')
            std::cout << "\\0 ";
        else
            std::cout << dest[i] << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. Copies h, i, then pads zeros up to 5 bytes.
  2. Byte index 5 ('f') was outside count, so it stays.
  3. Padding is a classic strncpy behavior.

Example 3 — Truncate and Null-Terminate

When copying a long source into a small buffer, add '\0' yourself.

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

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

    std::strncpy(dest, src, sizeof dest - 1);
    dest[sizeof dest - 1] = '\0';

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

How It Works

  1. Copy at most 5 characters into a 6-byte array.
  2. Force dest[5] = '\0' so it is a valid C-string.
  3. Without that step, printing could be unsafe.

Frequently Asked Questions

strncpy() copies at most count characters from one C-string into another.
dest is the array you copy into. source is the text you copy from. count is the maximum number of characters to write.
The same pointer as dest.
No. If the source is longer than count, dest is not given an ending null. Add one before you use dest as a C-string.
The remaining bytes up to count are filled with null characters.

Next: strpbrk()

Find the first character that matches any character in a set.

strpbrk() 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