strncat c: The function char *strncat(char *destination, const char *source, size_t n); appends first n characters of string pointed by source at the end of string pointed by destination. It will overwrite null character of destination string and append a null character at the end of combined string. Destination string should be large enough to contain the concatenated string.
Function prototype of strncat
char *strncat(char *destination, const char *source, size_t n);
- destination : This is pointer to destination array(containing a null terminated string) where we want to append characters from source string.
- source : It is a pointer to a string to be appended. This should not overlap with destination string.
- n : Number of characters to be appended.
Return value of strncat
This function returns a pointer to destination string(after appending characters from source string).
C program using strncat function
The following program shows the use of strncat function to append characters from one string at the end of another string.
#include <stdio.h> #include <string.h> #include <conio.h> int main() { char stringOne[100], stringTwo[100]; printf("Enter first string\n"); gets(stringOne); printf("Enter second string\n"); gets(stringTwo); strncat(stringTwo, stringOne, 5); printf("Second string after strncat\n%s", stringTwo); getch(); return(0); }
Output
Enter first string CrashCourse Enter second string Tech Second string after strncat TechCrash