BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C strncat() Function with example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: c-programming

In the last tutorial we discussed strcat() function, which is used for concatenation of one string to another string. In this guide, we will see a similar function strncat(), which is same as strcat() except that strncat() appends only the specified number of characters to the destination string.

C strncat() Function Declaration

char *strncat(char *str1, const char *str2, size_t n)

str1 – Destination string.
str2 – Source string which is appended at the end of destination string str1.
n – number of characters of source string str2 that needs to be appended. For e.g. if this is 5 then only the first 5 characters of source string str2 would be appended at the end of destination string str1.

Return value:
This function returns the pointer to the destination string str1.

Example: strncat() Function in C

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

int main () {
   char str1[50], str2[50];

   //destination string
   strcpy(str1, "This is my initial string");

   //source string
   strcpy(str2, ", add this");

   //displaying destination string
   printf("String after concatenation: %s\n", strncat(str1, str2, 5));

   // this should be same as return value of strncat()
   printf("Destination String str1: %s", str1);

   return 0;
}

Output:

String after concatenation: This is my initial string, add
Destination String str1: This is my initial string, add

As you can see that only 5 characters of string str2 are concatenated at the end of string str1 because we have specified the count as 5 in the strncat() function.

Another important point to note here is that this function returns the pointer to the destination string, which is why the when we displayed the returned value of strncat(), it is same as the destination string str1

Top Related Articles:

  1. C – loops in C programming with examples
  2. C strcmp() Function with example
  3. C – Pointer to Pointer (Double Pointer) with example
  4. C strcoll() Function – C tutorial
  5. Passing pointer to a function in C with example

Tags: C-Library, C-String

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap