Connecting Strings (Concatenation)

Concatenation is the process of appending one string onto another. It's kind of like addition for words. To perform concatenation, you use the strncat() function:

strncat(string1, string2, length);

This function will append the value of string2 onto the end of string1. It will append up to length number of characters. This length value should correspond to the remaining space in string1. For example:

char string1[25] = "Mary had a ";
char string2[] = "little lamb.";
strncat(string1, string2, 13);

strn* Functions and Overflow

In earlier C standards, the available string functions were strcat(), strcpy(), and so forth. The problem with these functions is that they allowed for a buffer overflow, a serious ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.