strcat(3)

strcat3

Concatenate strings

Usage

#include <string.h>

char *

strcat(char * restrict s, const char * restrict append);

char *

strncat(char * restrict s, const char * restrict append, size_t count);

Library

Standard C Library (libc, -lc)

Overview

The strcat() and strncat() functions append a copy of the null-terminated string append to the end of the null-terminated string s, then add a terminating ‘\0’. The string s must have sufficient space to hold the result. If s and append overlap, the results are undefined.

The strncat() function appends not more than count characters from append, and then adds a terminating ‘\0’. If s and append overlap, the results are undefined.

Return values

The strcat() and strncat() functions return the pointer s.

Security considerations

The strcat() function is easily misused in a manner which enables malicious users to arbitrarily change a running program's functionality through a buffer overflow attack.

Avoid using strcat(). Instead, use strncat() or strlcat() and ensure that no more characters are copied to the destination buffer than it can hold.

Note that strncat() can also be problematic. It may be a security concern for a string to be truncated at all. Since the truncated string will not be as long as the original, it may refer to a completely different resource and usage of the truncated resource could result in very incorrect behavior. Example:

void
foo(const char *arbitrary_string)
{
	char onstack[8];
#if defined(BAD)
	/*
	 * This first strcat is bad behavior.  Do not use strcat!
	 */
	(void)strcat(onstack, arbitrary_string);	/* BAD! */
#elif defined(BETTER)
	/*
	 * The following two lines demonstrate better use of
	 * strncat().
	 */
	(void)strncat(onstack, arbitrary_string,
	    sizeof(onstack) - strlen(onstack) - 1);
#elif defined(BEST)
	/*
	 * These lines are even more robust due to testing for
	 * truncation.
	 */
	if (strlen(arbitrary_string) + 1 >
	    sizeof(onstack) - strlen(onstack))
		err(1, "onstack would be truncated");
	(void)strncat(onstack, arbitrary_string,
	    sizeof(onstack) - strlen(onstack) - 1);
#endif
}

Releases

87 releases since FreeBSD 2.0.5-RELEASE; the text changed 41 times.

  1. 2.0.5
  2. 2.1.5–3.4
  3. 3.5–3.5.1
  4. 4.0
  5. 4.1–4.2
  6. 4.3–4.4
  7. 4.5–4.11
  8. 5.0–6.3
  9. 6.4
  10. 7.0
  11. 7.1
  12. 7.2
  13. 7.3
  14. 7.4
  15. 8.0
  16. 8.1
  17. 8.2
  18. 8.3
  19. 8.4
  20. 9.0
  21. 9.1
  22. 9.2
  23. 9.3
  24. 10.0
  25. 10.1
  26. 10.2
  27. 10.3
  28. 10.4
  29. 11.0
  30. 11.1
  31. 11.2
  32. 11.3
  33. 11.4
  34. 12.0
  35. 12.1
  36. 12.2
  37. 12.3
  38. 12.4
  39. 13.0–13.1
  40. 13.2
  41. 13.3–14.5
  42. 15.0–16.0

Standards

The strcat() and strncat() functions conform to ISO/IEC 9899:1990 (“ISO C90”).

History

The strcat() function first appeared in the Programmer's Workbench (PWB/UNIX) and was ported to Version 7 AT&T UNIX; strncat() first appeared in Version 7 AT&T UNIX.

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.