Skip to content
Tutorials Center
My learning

Topics

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.

C
#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

C Building a string

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.

C Checking the space first, and the cost of repeated calls

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

C
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 because buf is not a string yet. Start with buf[0] = '\0' or = "".
  • strncat's n is not the buffer size. It limits how many source characters are copied. The safe call is strncat(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.
  • strncat always terminates, unlike strncpy. 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 strcat is quadratic (the "Shlemiel the painter" problem). Keep a write position, as with snprintf above, or use memcpy at the known end.
  • Overlap is forbidden: strcat(s, s) is undefined behavior.
  • snprintf is often cleaner for building a string from several parts, formatted numbers included. See sprintf and snprintf.
  • Truncation is silent. With strncat you cannot tell whether the source was cut off, unless you compare lengths yourself.
  • GCC warnings like -Wstringop-overflow can catch constant-size mistakes at compile time.