Syntax
#include <string.h> char *strncat(char *string1, const char *string2, size_t count);Description
strncat appends the first count bytes of string2 to string1 and ends the resulting string with a null byte (\0). If count is greater than the length of string2, the length of string2 is used in place of count.
The strncat function operates on null-terminated strings. The string argument to the function should contain a null byte (\0) marking the end of the string.
strncat returns a pointer to the joined string (string1).
This example demonstrates the difference between strcat and strncat. strcat appends the entire second string to the first, whereas strncat appends only the specified number of bytes in the second string to the first.
#include <stdio.h> #include <string.h> #define SIZE 40 int main(void) { char buffer1[SIZE] = "computer"; char *ptr; /* Call strcat with buffer1 and " program" */ ptr = strcat(buffer1, " program"); printf("strcat : buffer1 = \"%s\"\n", buffer1); /* Reset buffer1 to contain just the string "computer" again */ memset(buffer1, '\0', sizeof(buffer1)); ptr = strcpy(buffer1, "computer"); /* Call strncat with buffer1 and " program" */ ptr = strncat(buffer1, " program", 3); printf("strncat: buffer1 = \"%s\"\n", buffer1); return 0; /**************************************************************************** The output should be: strcat : buffer1 = "computer program" strncat: buffer1 = "computer pr" ****************************************************************************/ }Related Information