String concatenation in C joins two strings end-to-end into a single result. The standard library provides strcat() for this, but writing it manually teaches how C strings work: they are null-terminated char arrays, and concatenation means walking to the end of the destination string then copying source characters one by one until the null terminator. Understanding the manual approach makes strcat() easier to use correctly — and helps avoid the buffer-overflow mistakes that come from not sizing the destination array.
How It Works — Step by Step
To concatenate "Hello" and "World":
- Start with
dest = "Hello\0"andsrc = "World\0". - Walk
iforward untildest[i] == '\0'. Nowi = 5(points past the'o'). - Copy each character of
src: dest[5]=’W’, dest[6]=’o’, dest[7]=’r’, dest[8]=’l’, dest[9]=’d’. - Write
dest[10] = '\0'to terminate the result. - Result:
"HelloWorld\0"
C Program for String Concatenation
/* String concatenation in C — custom function and strcat
* Compile: gcc -ansi -Wall -Wextra concat.c -o concat */
#include <stdio.h>
#include <string.h>
#define MAX 100
void str_concat(char dest[], const char src[])
{
int i = 0, j = 0;
while (dest[i] != '\0') /* find end of dest */
i++;
while (src[j] != '\0') { /* append src */
dest[i] = src[j];
i++;
j++;
}
dest[i] = '\0'; /* null-terminate */
}
int main(void)
{
char s1[MAX], s2[MAX], buf[MAX * 2];
printf("Enter first string: ");
scanf("%99s", s1);
printf("Enter second string: ");
scanf("%99s", s2);
/* Method 1: custom function */
strcpy(buf, s1);
str_concat(buf, s2);
printf("Custom concat: %s\n", buf);
/* Method 2: standard strcat */
strcpy(buf, s1);
strcat(buf, s2);
printf("strcat result: %s\n", buf);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra concat.c -o concat
./concat
Sample Input and Output
Test 1:
Enter first string: Hello Enter second string: World Custom concat: HelloWorld strcat result: HelloWorld
Test 2:
Enter first string: C Enter second string: Programming Custom concat: CProgramming strcat result: CProgramming
Code Explanation
- First while loop in str_concat — advances
ito the position ofdest‘s null terminator. This is where the appended characters will start. - Second while loop — copies each character of
srcintodeststarting at positioni, incrementing both indices together. - dest[i] = ‘\0’ — manually terminates the result. Without this, the string would run into whatever garbage follows in memory.
- buf[MAX * 2] — the destination buffer is sized to hold both strings. A common source of bugs is sizing the destination to only one string length; the result of concatenating two MAX-length strings needs at least
MAX * 2bytes. - scanf(“%99s”, s1) — the width limit prevents buffer overflow;
%99sreads at most 99 characters into a 100-byte array, leaving room for the null terminator.
What This Program Teaches
- Null-terminated strings — C strings end with
'\0'; every string operation depends on finding or writing this terminator correctly. - Walking a string with an index — advancing an integer index until the null terminator is the fundamental C idiom for string traversal.
- Buffer sizing — the destination must be large enough to hold the combined result; most string bugs come from undersized buffers.
- Standard vs custom — comparing your own implementation against the library function verifies understanding and confirms the output is identical.
Related Programs
- Copy and Concatenate Strings Without Standard Functions in C
- Compare Strings in C
- Reverse a String Using Pointers in C
- Anagram Program in C
- String Functions in C – Complete Reference
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.