YouTip LogoYouTip

C Function Strcat

## C Library Function - `strcat()` The `strcat()` function is a built-in function in the C standard library (``) used for string concatenation. It appends a copy of the source string to the end of the destination string. --- ## Description The C library function `char *strcat(char *dest, const char *src)` appends the string pointed to by `src` to the end of the string pointed to by `dest`. During this process: 1. The null terminator (`\0`) at the end of `dest` is overwritten by the first character of `src`. 2. The remaining characters of `src` are appended sequentially. 3. A new terminating null character (`\0`) is automatically added to the end of the newly formed concatenated string in `dest`. --- ## Declaration Following is the declaration for the `strcat()` function: ```c char *strcat(char *dest, const char *src); ``` --- ## Parameters * **`dest`** -- This is a pointer to the destination array, which must contain a null-terminated C string and must be large enough to hold the combined concatenated result. * **`src`** -- This is a pointer to the source null-terminated C string that is to be appended. It will not modify the original source string. --- ## Return Value The function returns a pointer to the final destination string (`dest`). --- ## Code Example The following example demonstrates how to use the `strcat()` function to concatenate two strings. ```c #include #include int main() { char src, dest; // Initialize the source and destination arrays strcpy(src, "This is source"); strcpy(dest, "This is destination"); // Concatenate src to the end of dest strcat(dest, src); // Print the final concatenated string printf("Final destination string: |%s|\n", dest); return 0; } ``` ### Output When you compile and run the above program, it produces the following output: ```text Final destination string: |This is destinationThis is source| ``` --- ## Important Considerations & Security Risks While `strcat()` is straightforward to use, it comes with critical security and stability considerations that every C developer must know: ### 1. Buffer Overflow Risk The `strcat()` function does not check the size of the destination buffer. If the `dest` array is not large enough to hold both its original contents and the appended `src` string (plus the null terminator `\0`), it will write past the boundary of the array. This causes a **buffer overflow**, leading to undefined behavior, data corruption, or security vulnerabilities (such as arbitrary code execution). ### 2. Safe Alternatives To prevent buffer overflows, modern C development recommends using safer alternatives: * **`strncat()`**: Allows you to specify the maximum number of characters to append. ```c strncat(dest, src, sizeof(dest) - strlen(dest) - 1); ``` * **`strlcat()`** (available on BSD-based systems and macOS): Automatically handles null-termination and ensures the buffer size is not exceeded. ### 3. No Overlapping Memory The behavior of `strcat()` is undefined if the memory regions pointed to by `dest` and `src` overlap. Do not attempt to append a string to itself using `strcat()`.
← C Function StrncmpC Function Memset β†’