strncpy() Function

The strncpy() function in C copies the first specified number of characters from the source string to the destination array.


Syntax of strncpy()

</>
Copy
char *strncpy(char *destination, const char *source, size_t num);

Parameters

ParameterDescription
destinationPointer to the destination array where the content is to be copied.
sourceC string to be copied.
numMaximum number of characters to be copied from source. (The type size_t is an unsigned integral type.)

The strncpy() function in C copies the first num characters from the source string to the destination array.

If the end of the source C string (indicated by a null-character) is encountered before num characters are copied, the destination is padded with null characters until exactly num characters have been written.

Note that if the source string is longer than num, no null-character is automatically appended, and the destination will not be a properly null-terminated C string.

Return Value

The function returns a pointer to the destination string.

Exceptions

The strncpy() function does not automatically append a null-character if the source string is longer than num. Additionally, if the destination and source arrays overlap, the behavior is undefined (for overlapping regions, consider using memmove() instead).


Examples for strncpy()

1. Copying with Padding

This example demonstrates how to use strncpy() to copy a string and pad the destination with null characters when the source string is shorter than the specified number of characters.

Program

</>
Copy
#include <stdio.h>
#include <string.h>

int main() {
    char src[] = "Hello";
    char dest[10];

    // Copy from src to dest with padding
    strncpy(dest, src, sizeof(dest));

    printf("Copied string: %s\n", dest);
    return 0;
}

Explanation:

  1. A character array src is initialized with the string "Hello".
  2. A destination array dest is declared with enough space to hold 10 characters.
  3. The strncpy() function copies the content of src into dest and pads the remaining bytes with null characters.
  4. The resulting string stored in dest is printed using printf().

Output:

Copied string: Hello

2. Copying Without Automatic Null Termination

This example demonstrates how strncpy() behaves when the source string is longer than the specified number of characters. Since no null terminator is added automatically in such cases, a manual null termination is required.

Program

</>
Copy
#include <stdio.h>
#include <string.h>

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

    // Copy the first 6 characters from src to dest
    strncpy(dest, src, 6);

    // Manually add a null terminator since strncpy does not append one automatically
    dest[5] = '\0';

    printf("Copied string: %s\n", dest);
    return 0;
}

Explanation:

  1. A character array src is initialized with the string "Hello, World!".
  2. A destination array dest is declared with space for 6 characters.
  3. The strncpy() function copies the first 6 characters from src to dest, but does not automatically append a null terminator.
  4. A null terminator is manually added to ensure that dest is a proper C string.
  5. The resulting string stored in dest is printed using printf().

Output:

Copied string: Hello