Home C++ Strings Functions strncpy() C++ strncpy() Function 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.
strncpy(dest, "Hello", 5); Call What 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 char* strncpy(char* destination, const char* source, size_t count); Parameters Parameter Description dest Required. The array you copy into. It must hold at least count characters. source Required. The characters you copy from. count Required. The most characters to write.
Return Value Return Value Description dest The same pointer you passed in.
C++ Examples 1. Basic copy 2. Short source 3. Truncate safely Example 1 — Basic strncpy() Copy a short greeting into a larger buffer.
#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 Source is shorter than count. '\0' from source is copied, then extra zeros pad to count.Printing dest is safe because it is terminated. Example 2 — Short Source Pads with Zeros See that leftover slots become '\0'.
#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 Copies h, i, then pads zeros up to 5 bytes. Byte index 5 ('f') was outside count, so it stays. Padding is a classic strncpy behavior. Example 3 — Truncate and Null-Terminate When copying a long source into a small buffer, add '\0' yourself.
#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 Copy at most 5 characters into a 6-byte array. Force dest[5] = '\0' so it is a valid C-string. Without that step, printing could be unsafe. Frequently Asked Questions What does strncpy() do in C++? strncpy() copies at most count characters from one C-string into another.
What are the parameters of strncpy()? dest is the array you copy into. source is the text you copy from. count is the maximum number of characters to write.
What does strncpy() return? The same pointer as dest.
Does strncpy() always add a null character? 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.
What happens when the source is shorter than count? The remaining bytes up to count are filled with null characters.
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.
8 people found this page helpful
Helpful Share Copy link Suggestion