strncat() in C - Source Code Example

In this source code example, we will see how to use the strncat() function in C programming with an example.

strncat() Function Overview

The strncat() function is part of the C standard library and is used to concatenate a specified number of characters from one string to another. This is particularly useful when you want to add only a portion of a source string to a destination string. 

 Key Points: 

- Found in the <string.h> header file. 

- The function appends the first n characters of the source string to the destination string. 

- The destination string must have enough space to store the concatenated result. 

- If the length of the source string is less than n, the entire source string is concatenated.

Source Code Example

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

int main() {
    char dest[50] = "Hello, ";
    char src[] = "world! Good day.";

    // Using strncat to concatenate the first 6 characters of src onto dest
    strncat(dest, src, 6);
    printf("%s\n", dest);

    // Concatenating a few more characters
    strncat(dest, src+6, 5);
    printf("%s\n", dest);

    return 0;


Hello, world!
Hello, world! Good


1. We define a dest string with the initial content "Hello, " and a src string containing "world! Good day.".

2. Using strncat(), we append the first 6 characters from the src string to the dest string, resulting in "Hello, world!".

3. To demonstrate further, we append the next 5 characters from the src string (starting just after "world!") to the dest string. This results in "Hello, world! Good".