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()
char *strncpy(char *destination, const char *source, size_t num);
Parameters
Parameter | Description |
---|---|
destination | Pointer to the destination array where the content is to be copied. |
source | C string to be copied. |
num | Maximum 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
#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:
- A character array
src
is initialized with the string"Hello"
. - A destination array
dest
is declared with enough space to hold 10 characters. - The
strncpy()
function copies the content ofsrc
intodest
and pads the remaining bytes with null characters. - The resulting string stored in
dest
is printed usingprintf()
.
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
#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:
- A character array
src
is initialized with the string"Hello, World!"
. - A destination array
dest
is declared with space for 6 characters. - The
strncpy()
function copies the first 6 characters fromsrc
todest
, but does not automatically append a null terminator. - A null terminator is manually added to ensure that
dest
is a proper C string. - The resulting string stored in
dest
is printed usingprintf()
.
Output:
Copied string: Hello