strncat function(Bounded String Concatenation) in C Language – example and detail

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

The strncat feature will give up copying when a null personality is encountered or n characters have been copied.

Syntax

The syntax for the strncat characteristic 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 wide variety of characters to be appended.

Returns

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

Required Header

In the C Language, the required header for the strncat feature 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 seem to be at an example to see how you would use the strncat feature 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 software will output:

TechOnTheNet.com is over 10 years old.

Similar Functions

Other C functions that are similar to the strncat function:

strcat function <string.h>