strcat() vs strncat() in C++

16 Aug, 2021
strcat()
The strcat() function will append a copy of the source string to the end of destination string. The strcat() function takes two arguments: 1) dest 2) src It will append copy of the source string in the destination string. The terminating character at the end of dest is replaced by the first character of src . Return value: The strcat() function returns dest, the pointer to the destination string.  
CPP
// CPP program to demonstrate// strcat#include <cstring>#include <iostream>using namespace std;int main(){    char dest[50] = "This is an";    char src[50] = " example";     strcat(dest, src);    cout << dest ;    return 0;}
Output:
This is an example
 
strncat()
The strncat() function in C++ appends the given number of character from one string to the end of another string.The strncat() function will take these three arguments: 1) Dest 2) Src 3) CountThis will append the given number of characters from src string to the end of dest string. The terminating character at the end of dest string will be replaced by the first character of src string . Return value: The strncat() function returns dest, the pointer to the destination string.  
CPP
// CPP program to demonstrate// strncat#include <cstring>#include <iostream> using namespace std; int main(){    char dest[25] = "This is an example";    char src[50] = " to show working of strncat() this is not added";     strncat(dest, src, 29);    cout << dest ;    return 0; }
Output: 
This is an example to show working of strncat()
 
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. 
CPP
// C,C++ program demonstrate difference between// 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 Pranav. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Akanksha_Rai
kapoorsagar226
C-String
cpp-string
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Multidimensional Arrays in C / C++
Function Pointer in C
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
Vector in C++ STL
Initialize a vector in C++ (7 different ways)
Templates in C++ with Examples
Operator Overloading in C++
Inheritance in C++