C strncat() method

This function appends up to n characters from one string to another.

Syntax:


char *strncat(char *dest, const char *src, size_t n);

Parameters:

  • dest: A pointer to the destination string.
  • src: A pointer to the source string.
  • n: The maximum number of characters to append.

Return Value:

A pointer to the destination string (dest).

Example:


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

int main() {
    char str1[10] = "My";
    char str2[] = "World";
    
    strncat(str1, str2, 2);  // Append first 2 characters
    printf("Concatenated string: %s\n", str1);  // Output: My Wor
    
    return 0;
}

Output:

Concatenated string: MyWo