strncat function in C Programming

If you understand the strcat function in C programming, then a question may appear in your subconscious (not sure) mind. That is about the concatenation of certain amount of two strings. For such kind of concatenation we can use another function defined in string.h header file. That is strncat function. Its basic form is given below:

**strncat(str1, str2, n);**

This function contains three parameters โ€“ str1 & str2 are two strings and n indicates an integer. Please consider the following program:

#include <stdio.h>
#include <string.h>

int main()
    char str1[50] = "Bangladesh ";
    char str2[50] = "Winning";
    strncat(str1, str2, 3);
    return 0;


Bangladesh win

Now let me explain the program. Here the left-most 3 characters of str2 are appended to str1. Please check the basic form again:

**strncat(str1, str2, n);**

Here n indicates how many left-most characters of str2 will be appended to str1. If n is 3, then left-most 3 characters will be appended. If n is 7, then left-most 7 characters will be appended.

That’s all about strncat function. Happy Coding!!!

comments powered by Disqus