This documentation is copyright © 1998-2001 Sandro Sigala <sandro@sigala.it>.
All rights reserved.

Released under the GNU General Public License.

Return to index Return to header Previous symbol Next symbol

strcat

Prototype

#include <string.h>

char *strcat(char *dest, const char *src);

Description

Appends the src string to the dest string overwriting the terminating null character at the end of dest, and then adds a terminating null character. The strings should not overlap, and the dest buffer size should be greater or equal to strlen(dest) + strlen(str) + 1. Returns the dest parameter.
For example, in the following code we catenate two strings:
char buf[8];
strcpy(buf, "desk");
strcat(buf, "top");
this is the state of the buf array before and after the call of strcat (? means any character):

Implementation

View source
#include <string.h>

char *strcat(char *dest, const char *src)
{
    char *dp = dest;

    while (*dp != '\0')
	++dp;
    while ((*dp++ = *src++) != '\0')
	;

    return dest;
}

Example

View source
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char buf[7];

    strcpy(buf, "abc");
    strcat(buf, "def");
    printf("%s\n", buf);         /* Prints "abcdef". */
    printf("%d\n", strlen(buf)); /* Prints "6". */

    return EXIT_SUCCESS;
}

References

ISO C 9899:1990 7.11.3.1