Manpage of STRCAT

STRCAT

Section: Linux Programmer's Manual (3)
Updated: 2016-07-17
Index
 

NAME

strcat, strncat - concatenate two strings  

SYNOPSIS

#include <string.h>char *strcat(char *dest, const char *src);char *strncat(char *dest, const char *src, size_t n);
 

DESCRIPTION

The strcat() function appends the srcstring to the deststring, overwriting the terminating null byte (aq\0aq) at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the deststring must have enough space for the result. If destis not large enough, program behavior is unpredictable; buffer overruns are a favorite avenue for attacking secure programs.

The strncat() function is similar, except that

*
it will use at most nbytes from src; and
*
srcdoes not need to be null-terminated if it contains nor more bytes.

As with strcat(), the resulting string in destis always null-terminated.

If srccontains nor more bytes, strncat() writes n+1bytes to dest(nfrom srcplus the terminating null byte). Therefore, the size of destmust be at least strlen(dest)+n+1.

A simple implementation of strncat() might be:

char *
strncat(char *dest, const char *src, size_t n)
{
    size_t dest_len = strlen(dest);
    size_t i;

    for (i = 0 ; i < n && src[i] != aq\0aq ; i++)
        dest[dest_len + i] = src[i];
    dest[dest_len + i] = aq\0aq;

    return dest;
}
 

RETURN VALUE

The strcat() and strncat() functions return a pointer to the resulting string dest.  

ATTRIBUTES

For an explanation of the terms used in this section, see attributes(7).
InterfaceAttributeValue
strcat(), strncat() Thread safetyMT-Safe
 

CONFORMING TO

POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.  

NOTES

Some systems (the BSDs, Solaris, and others) provide the following function:


    size_t strlcat(char *dest, const char *src, size_t size);

This function appends the null-terminated string srcto the string dest, copying at most size-strlen(dest)-1from src, and adds a terminating null byte to the result, unlesssizeis less than strlen(dest). This function fixes the buffer overrun problem of strcat(), but the caller must still handle the possibility of data loss if sizeis too small. The function returns the length of the string strlcat() tried to create; if the return value is greater than or equal to size, data loss occurred. If data loss matters, the caller musteither check the arguments before the call, or test the function return value. strlcat() is not present in glibc and is not standardized by POSIX, but is available on Linux via the libbsdlibrary.  

EXAMPLE

Because strcat() and strncat() must find the null byte that terminates the string destusing a search that starts at the beginning of the string, the execution time of these functions scales according to the length of the string dest. This can be demonstrated by running the program below. (If the goal is to concatenate many strings to one target, then manually copying the bytes from each source string while maintaining a pointer to the end of the target string will provide better performance.)  

Program source

#include <string.h>
#include <time.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
#define LIM 4000000
    int j;
    char p[LIM];
    time_t base;

    base = time(NULL);
    p[0] = aq\0aq;

    for (j = 0; j < LIM; j++) {
        if ((j % 10000) == 0)
            printf("%d %ld\n", j, (long) (time(NULL) - base));
        strcat(p, "a");
    }
}
. 

SEE ALSO

bcopy(3), memccpy(3), memcpy(3), strcpy(3), string(3), strncpy(3), wcscat(3), wcsncat(3)


 

Index

NAME
SYNOPSIS
DESCRIPTION
RETURN VALUE
ATTRIBUTES
CONFORMING TO
NOTES
EXAMPLE
Program source
SEE ALSO

This document was created by man2html, using the manual pages.
Time: 16:30:26 GMT, October 09, 2016 Click Here!