YouTip LogoYouTip

C Function Strcpy

## C Library Function - `strcpy()` The `strcpy()` function is a built-in function in the C standard library defined in the `` header file. It is used to copy a source string to a destination character array. --- ## Description The C library function `char *strcpy(char *dest, const char *src)` copies the null-terminated string pointed to by `src` (including the terminating null character `\0`) to the buffer pointed to by `dest`. > **Critical Warning:** The `strcpy()` function does not perform bounds checking. If the destination buffer `dest` is not large enough to hold the source string `src`, it will cause a **buffer overflow**, which can lead to undefined behavior, program crashes, or security vulnerabilities. --- ## Declaration Following is the declaration for the `strcpy()` function: ```c char *strcpy(char *dest, const char *src) ``` --- ## Parameters * **`dest`** β€” A pointer to the destination array where the content is to be copied. * **`src`** β€” The null-terminated string to be copied. --- ## Return Value The function returns a pointer to the destination string `dest`. --- ## Code Examples ### Example 1: Basic Usage The following example demonstrates how to copy a string literal into a character array, and how to copy one character array to another. ```c #include #include int main() { char src; char dest; // Initialize the destination buffer with null characters memset(dest, '\0', sizeof(dest)); // Copy a string literal into the src array strcpy(src, "This is YouTip.com"); // Copy the contents of src into the dest array strcpy(dest, src); printf("Final destination string: %s\n", dest); return 0; } ``` #### Output ```text Final destination string: This is YouTip.com ``` --- ### Example 2: Copying Multiple Strings This example shows how to initialize and copy multiple strings using `strcpy()`. ```c #include #include int main () { char str1[] = "Sample string"; char str2; char str3; // Copy str1 into str2 strcpy(str2, str1); // Copy a string literal into str3 strcpy(str3, "copy successful"); printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3); return 0; } ``` #### Output ```text str1: Sample string str2: Sample string str3: copy successful ``` --- ## Important Considerations & Best Practices When using `strcpy()`, keep the following safety guidelines in mind: ### 1. Buffer Overflow Risk `strcpy()` will continue copying characters from `src` until it encounters the null terminator (`\0`). If `dest` is smaller than `src`, `strcpy` will write past the boundary of `dest`, corrupting adjacent memory. ### 2. Overlapping Memory The behavior of `strcpy()` is undefined if the source and destination strings overlap in memory. If memory overlap is a possibility, use `memmove()` instead. ### 3. Safer Alternatives To prevent buffer overflows, modern C development highly recommends using safer alternatives: * **`strncpy(dest, src, n)`**: Copies up to `n` characters. *Note: If `src` is longer than `n`, the destination string will not be null-terminated automatically.* * **`strcpy_s(dest, dest_size, src)`**: Introduced in C11 (Annex K), this function takes the destination size as an argument and returns an error code if an overflow is detected. * **`strlcpy(dest, src, size)`**: Available on BSD-derived systems (including macOS), this function guarantees null-termination and prevents buffer overflows.
← C Function StrncpyC Function Strcoll β†’