Jump to: navigation, search

Description

The strncat() function appends not more than n characters (a null character and characters that follow it are not appended) from the array pointed to by src to the end of the string pointed to by dest. The initial character of src overwrites the null character at the end of dest. A terminating null character is always appended to the result. Thus, dest should accomodate strlen(src)+n+1 characters. Source and destination may not overlap.

Return value

The strncat() function shall return the pointer dest; the function has no failure mode and no error return.

Prototype

Declared in string.h

The C90 prototype is:

char *strncat(char *dest, const char *src, size_t n);

The C99 prototype adds the new restrict qualifier:

char *strncat(char * restrict dest, const char * restrict src, size_t n);

Implementation

In standard C, this can be implemented as (adjust prototype for C99):

#include <stddef.h> /* size_t */
char *strncat(char *dest, const char *src, size_t n)
{
    char *ret = dest;
    while (*dest)
        dest++;
    while (n--)
        if (!(*dest++ = *src++))
            return ret;
    *dest = 0;
    return ret;
}

Compilable unit, portable C90 in implementation namespace; public domain; past reviewers: none; current reviews: none

References

The C Standard, 7.21.3.2 (C99)

Personal tools