YouTip LogoYouTip

C Function Strncpy

## C Library Function - strncpy() The `strncpy()` function is a built-in C library function defined in the `` header file. It is used to copy a specified number of characters from a source string to a destination buffer. Unlike `strcpy()`, `strncpy()` allows you to specify the maximum number of characters to copy, which helps prevent buffer overflow vulnerabilities when used correctly. --- ## Syntax ```c char *strncpy(char *dest, const char *src, size_t n); ``` ### Parameters * **`dest`**: A pointer to the destination array where the content is to be copied. * **`src`**: The source string to be copied. * **`n`**: The maximum number of characters to copy from the source string. ### Return Value The function returns a pointer to the destination string (`dest`). --- ## How strncpy() Works The behavior of `strncpy()` depends on the length of the source string (`src`) relative to the limit `n`: 1. **If `strlen(src)` is less than `n`**: The function copies all characters from `src` to `dest`, including the terminating null character (`\0`). The remaining bytes in `dest` (up to `n`) are padded with additional null characters (`\0`). 2. **If `strlen(src)` is equal to or greater than `n`**: The function copies exactly `n` characters to `dest`. **Crucially, no null-terminator (`\0`) is appended to `dest` automatically.** --- ## Code Examples ### Example 1: Basic Usage (Safe Copying) In this example, we copy the first 10 characters of the source string into a pre-cleared destination buffer. ```c #include #include int main() { char src; char dest; // Initialize the destination buffer with null bytes memset(dest, '\0', sizeof(dest)); // Populate the source string strcpy(src, "This is runoob.com"); // Copy up to 10 characters from src to dest strncpy(dest, src, 10); printf("Final destination string: %s\n", dest); return 0; } ``` #### Output ```text Final destination string: This is ru ``` --- ### Example 2: Handling the Missing Null-Terminator If the source string is longer than or equal to `n`, `strncpy()` will not null-terminate the destination string. This can lead to undefined behavior or memory leaks when printing or reading the string. The example below demonstrates how to manually ensure null-termination: ```c #include #include int main() { char src[] = "Hello, World!"; char dest; // Buffer size is 6 // Copy 5 characters. dest will NOT be null-terminated by strncpy() strncpy(dest, src, 5); // Manually null-terminate the string at the last index dest = '\0'; printf("Safely terminated string: %s\n", dest); return 0; } ``` #### Output ```text Safely terminated string: Hello ``` --- ## Important Considerations and Best Practices * **The Null-Termination Pitfall**: Always remember that if `n` is less than or equal to the length of `src`, the resulting string in `dest` will **not** be null-terminated. To prevent security vulnerabilities and crashes, always manually set the last byte of your destination buffer to `\0` after calling `strncpy()`. * **Performance Overhead**: If `n` is much larger than the length of `src`, `strncpy()` will fill the entire remaining buffer with `\0` bytes. This can lead to performance overhead in time-critical applications. * **Modern Alternatives**: In modern C development, consider using safer alternatives if supported by your platform: * `strncpy_s()` (introduced in C11) * `strlcpy()` (available on BSD, macOS, and Linux via `libbsd`)
← C Function StrcspnC Function Strcpy β†’