The strncat() function appends a specified number of characters from one string to the end of another. It is declared in the <string.h> header file.
- It appends at most n characters from the source string and adds a null terminator ('\0').
- The destination array must have enough space to hold the concatenated string; otherwise, the behavior is undefined.
#include <stdio.h>
#include <string.h>
int main() {
char dest[20] = "Hello ";
char src[] = "World";
strncat(dest, src, 3);
printf("Result: %s\n", dest);
return 0;
}
Output
Result: Hello Wor
Syntax
char *strncat(char *dest, const char *src, size_t n)
Parameters
This method accepts the following parameters:Â
- dest: the string where we want to append.
- src: the string from which 'n' characters are going to append.
- n: represents a maximum number of characters to be appended. size_t is an unsigned integral type.
Return Value
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, let's say n=5.
Examples
Input: src = "world"
dest = "Hello "
Output: "Hello world"
Input: src = "efghijkl"
dest = "abcd"
Output: "abcdefghi"
Program
#include <cstring>
#include <iostream>
using namespace std;
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
cout <<"Source string : "<< src << endl;
cout <<"Destination string : "<< dest;
return 0;
}
// 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 : abcdefghiComparison of strncat() and strcat()
Both strcat() and strncat() are used to concatenate strings, but strncat() provides better control over the number of characters appended.
- strcat() appends the entire source string to the destination string until it encounters the null terminator ('\0').
- strncat() appends only up to n characters from the source string, helping reduce the risk of buffer overflow.
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
// Take any two strings
char src[50] = "forgeeks";
char dest1[50] = "geeks";
char dest2[50] = "geeks";
cout << "Before strcat() function execution, ";
cout << "destination string : "<< dest1 << endl;
// Appends the entire string of src to dest1
strcat(dest1, src);
// Prints the string
cout << "After strcat() function execution, ";
cout << "destination string : "<< dest1 << endl;
cout << "Before strncat() function execution, ";
cout << "destination string : "<< dest2 << endl;
// Appends 3 characters from src to dest2
strncat(dest2, src, 3);
// Prints the string
cout << "After strncat() function execution, ";
cout << "destination string : "<< dest2 << endl;
return 0;
}
// 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Â