C strncpy() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
string.h

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.

C
strncpy(dest, "Hello", 5);
CallWhat 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
C
char *strncpy(char *dest, const char *src, size_t n);

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
dest pointerThe same destination pointer you passed in.

C Examples

Example 1 — Short Source (Zero Padding)

When src is shorter than n, strncpy pads the rest of the n-byte region with '\0'.

C
#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

  1. Copy "Hi". Characters H, i, and the source '\0' are written.
  2. Pad to n=5. Two more '\0' bytes fill the remainder of the 5-byte write.
  3. Print safely. Because a terminator exists, %s works.
  4. 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'.

C
#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

  1. Cap at 5. sizeof dest - 1 is 5 for a 6-byte array.
  2. Copy five letters. H e l l o land in dest[0..4].
  3. Force terminator. dest[5] = '\0' makes a valid C-string.
  4. 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.

C
#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

  1. Copy 4 bytes. A B C D fill the whole array.
  2. No terminator. strncpy stopped after n with no room left for '\0'.
  3. Overwrite last byte. dest[3] = '\0' yields "ABC".
  4. Prefer Example 2. Cap with sizeof - 1 so you do not sacrifice a copied character accidentally.

Frequently Asked Questions

strncpy() copies at most n characters into dest. It may leave dest without a '\0' when the source is longer than n.
All three are required. dest, source, and count control the copy.
The same dest pointer you passed in.
Include <string.h>.
No. If n characters are copied from a longer source, you must add '\0' yourself.

Next: strnset()

Fill the first n characters of a string with one value.

strnset() 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.

9 people found this page helpful