totn C Functions

C Language: strncat function
(Bounded String Concatenation)

In the C Programming Language, the strncat function appends a copy of the string pointed to by s2 to the end of the string pointed to by s1. It returns a pointer to s1 where the resulting concatenated string resides.

The strncat function will stop copying when a null character is encountered or n characters have been copied.

Syntax

The syntax for the strncat function in the C Language is:

char *strncat(char *s1, const char *s2, size_t n);

Parameters or Arguments

s1
A pointer to a string that will be modified. s2 will be copied to the end of s1.
s2
A pointer to a string that will be appended to the end of s1.
n
The number of characters to be appended.

Returns

The strncat function returns a pointer to s1 (where the resulting concatenated string resides).

Required Header

In the C Language, the required header for the strncat function is:

#include <string.h>

Applies To

In the C Language, the strncat function can be used in the following versions:

  • ANSI/ISO 9899-1990

strncat Example

Let's look at an example to see how you would use the strncat function in a C program:

/* Example using strncat by TechOnTheNet.com */

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

int main(int argc, const char * argv[])
{
    /* Define an example variable capable of holding up to 100 characters */
    char example[100];

    /* Copy the string "TechOnTheNet.com " into the example variable */
    strcpy (example, "TechOnTheNet.com ");

    /* Concatenate the string in the example variable with 21 characters
     from the string "is over 10 years old." */
    strncat (example, "is over 10 years old.", 21);

    /* Display the contents of the variable to the screen */
    printf("%s\n", example);

    return 0;
}

When compiled and run, this application will output:

TechOnTheNet.com is over 10 years old.

Similar Functions

Other C functions that are similar to the strncat function: