C Reference ยท string.h
C strcat() and strncat() functions
Reference for C strcat() and strncat(): append one string to another, the buffer-size rules, the quadratic cost of repeated calls, and safer alternatives.
strcat() appends a copy of one string to the end of another. It finds the end of the destination by scanning for its terminator, then copies the source after it, including the source's terminator. strncat() appends at most n characters from the source and always adds a terminator.
The destination must already hold a valid string and have room for the combined text plus the terminator. Neither function checks this, so you must do the arithmetic yourself.
#include <string.h> char *strcat(char *restrict dest, const char *restrict src); char *strncat(char *restrict dest, const char *restrict src, size_t n);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
char *strcat(char *restrict dest, const char *restrict src); char *strncat(char *restrict dest, const char *restrict src, size_t n);
| Name | Description |
|---|---|
dest |
A null-terminated string in a buffer big enough for the result. |
src |
The string to append. |
n |
(strncat) The maximum number of characters to take from src. |
Returns: dest.
Errors / undefined behavior: the buffer must hold strlen(dest) + strlen(src) + 1 bytes (for strncat: strlen(dest) + min(n, strlen(src)) + 1). Otherwise the write overflows. An unterminated dest or overlapping buffers are also undefined.
Usage notes
- The destination must be initialized.
char buf[32]; strcat(buf, "x");appends to garbage becausebufis not a string yet. Start withbuf[0] = '\0'or= "". strncat'snis not the buffer size. It limits how many source characters are copied. The safe call isstrncat(dest, src, size - strlen(dest) - 1). This is easy to get wrong, which is why many programmers write the check by hand as shown above.strncatalways terminates, unlikestrncpy. It also does not pad with zeros.- Cost is proportional to the destination length because each call rescans from the start. Joining many pieces with repeated
strcatis quadratic (the "Shlemiel the painter" problem). Keep a write position, as withsnprintfabove, or usememcpyat the known end. - Overlap is forbidden:
strcat(s, s)is undefined behavior. snprintfis often cleaner for building a string from several parts, formatted numbers included. Seesprintfandsnprintf.- Truncation is silent. With
strncatyou cannot tell whether the source was cut off, unless you compare lengths yourself. - GCC warnings like
-Wstringop-overflowcan catch constant-size mistakes at compile time.