strncat() function in C/C++

3

In C++, strncat() is a predefined function used for string handling. string.h is the header file required for string functions.

This function appends not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. The initial character of string(src) overwrites the Null-character present at the end of string(dest). Thus, length of the string(dest) becomes strlen(dest)+n. But, if the length of the string(src) is less than n, only the content up to the terminating null-character is copied and length of the string(dest) becomes strlen(src) + strlen(dest).

The behavior is undefined if

  • the strings overlap.
  • the dest array is not large enough to append the contents of src.

Syntax:

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

Parameters:
dest is the string where we want to append.
src is the string from which 'n' characters are going to append.
n represents maximum number of character to be appended.
size_t is an unsigned integral type.

Return:
The strncat() function shall return the pointer to the string(dest).

Application
Given two strings src and dest in C++, we need to append ‘n’ character from src to dest, lets say n=5.

Examples:

Input:  src = "world"
        dest = "Hello "
Output: "Hello world"

Input:  src = "efghijkl"
        dest = "abcd"
Output: "abcdefghi"

// C,C++ program demonstrate functionality of strncat()
#include <stdio.h>
#include <string.h>

int main()
{
  
   // Take any two strings
   char src[50] = "efghijkl";
   char dest[50]= "abcd";
 
   // Appends 5 character from src to dest
   strncat(dest, src, 5);
    
   // Prints the string
   printf("Source string : %s\n", src);
   printf("Destination string : %s", dest);
   
   return 0;
}

Output:

Source string : efghijkl
Destination string : abcdefghi

How strncat() is different from strcat() ?

It is recommended by many of the programmers that strncat() is safe as compared to strcat() because strcat() does not check for the size of the copied data, and copies until it gets to a null terminator, it might cause a buffer overflow while strncat() check for the size of the copied data, and will copy only ‘n’ bytes.

// C,C++ program demonstrate difference bewteen 
// strncat() and strcat()
#include <stdio.h>
#include <string.h>

int main()
{
  
   // Take any two strings
   char src[50] = "forgeeks";
   char dest1[50] = "geeks";
   char dest2[50] = "geeks";
 
   printf("Before strcat() function execution, ");
   printf("destination string : %s\n", dest1);
   
   // Appends the entire string of src to dest1
   strcat(dest1, src);
 
   // Prints the string
   printf("After strcat() function execution, ");
   printf("destination string : %s\n", dest1);
 
   printf("Before strncat() function execution, ");
   printf("destination string : %s\n", dest2);
   
   // Appends 3 characters from src to dest2
   strncat(dest2, src, 3);
    
   // Prints the string
   printf("After strncat() function execution, ");
   printf("destination string : %s\n", dest2);
   
   return 0;
}

Output:

Before strcat() function execution, destination string : geeks
After strcat() function execution, destination string : geeksforgeeks
Before strncat() function execution, destination string : geeks
After strncat() function execution, destination string : geeksfor

This article is contributed by Akash Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 1 vote(s)