The strncpy() function copies at most a specified number of characters from one string to another. It is declared in the <string.h> header file.
- It copies up to n characters from the source string to the destination string.
- If the source string is shorter than n, the remaining characters in the destination are filled with '\0'.
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "GeeksforGeeks";
char destination[20];
strncpy(destination, source, 5);
destination[5] = '\0'; // Null-terminate the copied string
printf("Copied string: %s\n", destination);
return 0;
}
Output
Copied string: Geeks
Syntax
char *strncpy(char *dest, const char *src, size_t n);
Parameters
The strncpy(dest, src, n) function takes three parameters:
- dest: A pointer to the destination array where the content is to be copied.
- src: A pointer to the source string to be copied.
- n: The number of characters to be copied from the source string.
Return Value
The strncpy(dest, src, n) function returns a pointer to the destination string dest. If the length of src is less than n, the remainder of dest will be padded with null characters.
Examples of strncpy() in C
The below examples demonstrates the usage of strncpy() in different scenarios in C language.
Example: Program to demonstrates the basic usage of the strncpy() function to copy a specified number of characters from one string to another.
#include <stdio.h>
#include <string.h>
int main()
{
// Source string
char src[] = "Hello, World!";
// Destination string
char dest[20];
// Copy the first 5 characters from src to dest
strncpy(dest, src, 5);
// Null-terminate the destination string
dest[5] = '\0';
// Print the destination string
printf("Destination string: %s\n", dest);
return 0;
}
Output
Destination string: Hello
Example: Program to demonstrates how we can use strncpy() to copy a fixed length of characters from a source string to a destination string, ensuring the destination string is properly null-terminated.
#include <stdio.h>
#include <string.h>
void customStrncpy(char *dest, const char *src, size_t n)
{
strncpy(dest, src, n);
// Ensure the destination string is null-terminated
dest[n] = '\0';
}
int main()
{
// Source string
char src[] = "Programming in C is fun!";
// Destination string
char dest[20];
// Custom string copy with fixed length
customStrncpy(dest, src, 15);
// Print the destination string
printf("Custom copied string: %s\n", dest);
return 0;
}
Output
Custom copied string: Programming in
Example: Program to demonstrates how to use strncpy() to copy a fixed-length substring from the source string into the destination string.
#include <stdio.h>
#include <string.h>
int main()
{
// Source string
char src[] = "Boyer-Moore Algorithm";
// Destination string
char dest[20];
// Copy a fixed-length substring from src to dest
strncpy(dest, src + 6, 5);
// Null-terminate the destination string
dest[5] = '\0';
// Print the destination string
printf("Fixed-length substring: %s\n", dest);
return 0;
}
Output
Fixed-length substring: Moore
Points to Remember
strncpy() does not automatically append a null terminator if the source string length is greater than or equal to n. In such cases, you should manually add '\0' to the destination string.
- If the source string is shorter than n, strncpy() fills the remaining positions in the destination string with '\0'.
- Ensure the destination array has enough space to store the copied characters and the null terminator (if added manually).