Home C String Functions strncpy() C strncpy() Function Definition In C, strncpy() copies at most n characters from the source into the destination. If the source is shorter, the rest of the n bytes are filled with '\0'. If n runs out first, the destination may have no ending '\0'.
You pass destination, source, and count. All three are required. Add a '\0' yourself when the source can be longer than n. Include <string.h>.
Syntax Pass destination, source, and count. Start with this call.
strncpy(dest, "Hello", 5); Call What it does strncpy(dest, "Hello", 5)Copies up to 5 characters into dest. Add '\0' if the source may be longer.
Include <string.h>. After a full n-byte copy from a longer source, write dest[n - 1] = '\0' or leave an extra byte.
Library declarations char *strncpy(char *dest, const char *src, size_t n); 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 pointer The same destination pointer you passed in.
C Examples 1. Short src (padding) 2. Safe capped copy 3. Truncation pitfall Example 1 — Short Source (Zero Padding) When src is shorter than n, strncpy pads the rest of the n-byte region with '\0'.
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[8];
strncpy(dest, "Hi", 5);
printf("%s\n", dest);
return 0;
} How It Works Copy "Hi". Characters H, i, and the source '\0' are written.Pad to n=5. Two more '\0' bytes fill the remainder of the 5-byte write.Print safely. Because a terminator exists, %s works.Note. Padding writes zeros into dest—useful for fixed fields, overkill for simple text.Example 2 — Safe Capped Copy Leave one byte free, copy, then force dest[sizeof(dest) - 1] = '\0'.
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[6];
strncpy(dest, "HelloWorld", sizeof dest - 1);
dest[sizeof dest - 1] = '\0';
printf("%s\n", dest);
return 0;
} How It Works Cap at 5. sizeof dest - 1 is 5 for a 6-byte array.Copy five letters. H e l l o land in dest[0..4].Force terminator. dest[5] = '\0' makes a valid C-string.Result. Printing shows the truncated "Hello".Example 3 — Truncation Without Terminator (Pitfall) If n equals the full buffer size and src is longer, you get no '\0'. Fix it before using %s.
#include <stdio.h>
#include <string.h>
int main(void) {
char dest[4];
strncpy(dest, "ABCD", 4); /* no '\0' written */
dest[3] = '\0'; /* force terminator (last byte) */
printf("%s\n", dest);
return 0;
} How It Works Copy 4 bytes. A B C D fill the whole array.No terminator. strncpy stopped after n with no room left for '\0'.Overwrite last byte. dest[3] = '\0' yields "ABC".Prefer Example 2. Cap with sizeof - 1 so you do not sacrifice a copied character accidentally.Frequently Asked Questions What does strncpy() do in C? strncpy() copies at most n characters into dest. It may leave dest without a '\0' when the source is longer than n.
What are the parameters of strncpy()? All three are required. dest, source, and count control the copy.
What does strncpy() return? The same dest pointer you passed in.
Which header file does strncpy() need? Include <string.h>.
Does strncpy() always add '\0'? No. If n characters are copied from a longer source, you must add '\0' yourself.
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.
9 people found this page helpful
Helpful Share Copy link Suggestion