strcat() Append one string to another
strcat() Append one string to another
strcat() appends src over the old terminator and writes a new one. The buffer needs both lengths plus one byte; the strings must not overlap.
| Category | string.h strings |
|---|---|
| Kind | Library function |
| Header | <string.h> |
| C standard | C89 / C90 |
📝 Syntax
char *strcat(char *dest, const char *src);
⚙️ Parameters
| dest Required | A writable null-terminated destination with enough remaining capacity. |
|---|---|
| src Required | The null-terminated source string to append. |
Returns: Returns dest.
▶ Example
Edit the C code below and click Run; the server compiles and executes it:
Output:
Hello, C!
⚠️ Warning The destination capacity must hold the original length, appended length, and final null character. Overlapping dest and src causes undefined behavior.
💡 Tip The example reserves space for the terminator with strlen(text) + strlen(suffix) < sizeof text.
🏷️ Related string.h functions
📚 Related Tutorials
❓ FAQ
QHow large must the destination buffer be for strcat?
AIt needs at least strlen(dest) + strlen(src) + 1 bytes, with the final byte reserved for the new terminator.
QDoes strcat preserve the destination's original null terminator?
ANo. It begins overwriting at that position with the source string and writes a new null character after the combined result.
QWhy can repeatedly appending many strings with strcat become progressively slower?
AEach strcat call scans the destination to find its end. As the destination grows, repeatedly scanning from the beginning makes the total work grow quadratically. Track the current length or an end pointer instead; after confirming sufficient total capacity, write new content directly at the end and update the terminator.