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

C strncat() method – Interview Questions

Q 1: What is strncat()?
Ans: A safer version of strcat() that appends limited characters.
Q 2: What is the syntax of strncat()?
Ans: strncat(destination, source, n);
Q 3: Does strncat() add a null terminator?
Ans: Yes, it appends "\0".
Q 4: Is strncat() completely safe?
Ans: Safer than strcat(), but destination size must still be managed.
Q 5: Which header file is required?
Ans: .

C strncat() method – Objective Questions (MCQs)

Quiz not found.

Related C strncat() method Topics