| strcpy() | These functions copy the string pointed to by src, into a string at the buffer pointed to by dst. The programmer is responsible for allocating a destination buffer large enough, that is, strlen(src) + 1. For the difference between the two functions, see RETURN VALUE. |
| strcat() | This function catenates the string pointed to by src, after the string pointed to by dst (overwriting its terminating null byte). The programmer is responsible for allocating a destination buffer large enough, that is, strlen(dst) + strlen(src) + 1. |
| stpcpy() | This function returns a pointer to the terminating null byte of the copied string. |
| strcat() | These functions return dst. |
| stpcpy() | POSIX.1-2008. |
| strcat() | C11, POSIX.1-2008. |
| stpcpy() | POSIX.1-2008. |
| strcat() | POSIX.1-2001, C89, SVr4, 4.3BSD. |
NAME
stpcpy, strcpy, strcat - copy or catenate a string
LIBRARY
Standard C library (libc, -lc)
SYNOPSIS
#include <string.h>char *stpcpy(char *restrict \ndst\n, const char *restrict \nsrc\n);\n
\nchar *strcpy(char *restrict \ndst\n, const char *restrict \nsrc\n);\n
\nchar *strcat(char *restrict \ndst\n, const char *restrict \nsrc\n);stpcpy():
\n
Since glibc 2.10:
\n
_POSIX_C_SOURCE >= 200809L
\n
Before glibc 2.10:
\n
_GNU_SOURCEDESCRIPTION
- stpcpy()
- strcpy()
These functions copy the string pointed to by src, into a string at the buffer pointed to by dst. The programmer is responsible for allocating a destination buffer large enough, that is, strlen(src) + 1. For the difference between the two functions, see RETURN VALUE.
- strcat()
This function catenates the string pointed to by src, after the string pointed to by dst (overwriting its terminating null byte). The programmer is responsible for allocating a destination buffer large enough, that is, strlen(dst) + strlen(src) + 1.
An implementation of these functions might be:
char *
stpcpy(char *restrict dst, const char *restrict src)
{
\n
char *p;
\n
p = mempcpy(dst, src, strlen(src));
\n
*p = '\0';
\n
return p;
}
char *
strcpy(char *restrict dst, const char *restrict src)
{
\n
stpcpy(dst, src);
\n
return dst;
}
char *
strcat(char *restrict dst, const char *restrict src)
{
\n
stpcpy(dst + strlen(dst), src);
\n
return dst;
}RETURN VALUE
- stpcpy()
This function returns a pointer to the terminating null byte of the copied string.
- strcpy()
- strcat()
These functions return dst.
ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
| stpcpy (), strcpy (), strcat () | Thread safety | MT-Safe |
STANDARDS
- stpcpy()
POSIX.1-2008.
- strcpy()
- strcat()
C11, POSIX.1-2008.
STANDARDS
- stpcpy()
POSIX.1-2008.
- strcpy()
- strcat()
POSIX.1-2001, C89, SVr4, 4.3BSD.
CAVEATS
The strings src and dst may not overlap.
If the destination buffer is not large enough, the behavior is undefined. See _FORTIFY_SOURCE in feature_test_macros(7).
strcat() can be very inefficient. Read about Shlemiel the painter (opens in new tab).
EXAMPLES
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
\n
char *p;
\n
char *buf1;
\n
char *buf2;
\n
size_t len, maxsize;
\n
maxsize = strlen("Hello ") + strlen("world") + strlen("!") + 1;
\n
buf1 = malloc(sizeof(*buf1) * maxsize);
\n
if (buf1 == NULL)
\n
err(EXIT_FAILURE, "malloc()");
\n
buf2 = malloc(sizeof(*buf2) * maxsize);
\n
if (buf2 == NULL)
\n
err(EXIT_FAILURE, "malloc()");
\n
p = buf1;
\n
p = stpcpy(p, "Hello ");
\n
p = stpcpy(p, "world");
\n
p = stpcpy(p, "!");
\n
len = p - buf1;
\n
printf("[len = %zu]: ", len);
\n
puts(buf1); // "Hello world!"
\n
free(buf1);
\n
strcpy(buf2, "Hello ");
\n
strcat(buf2, "world");
\n
strcat(buf2, "!");
\n
len = strlen(buf2);
\n
printf("[len = %zu]: ", len);
\n
puts(buf2); // "Hello world!"
\n
free(buf2);
\n
exit(EXIT_SUCCESS);
}SEE ALSO
strdup(3), string(3), wcscpy(3), string_copying(7)