String Concatenation in C

most important concepts every C programmer must understand. It refers to the process of joining two or more strings into a single string. This topic is frequently asked in C programming interviews, exams, and real-world applications like input processing and text handling.

String Concatenation in C

In this tutorial, you will learn:

  • What string concatenation in C is
  • Different ways to concatenate strings
  • Using built-in functions and manual methods
  • Complete C programs with explanations

What is String Concatenation in C?

String concatenation in C means appending one string to the end of another string.

Example:

String 1: "Hello"
String 2: "World"
Result: "HelloWorld"

Since C does not support strings as a built-in data type, strings are handled using character arrays, making concatenation slightly different from other languages.


Ways to Perform String Concatenation in C

There are three common methods for string concatenation in C:

  1. Using strcat() function
  2. Using strncat() function
  3. Without using any library function (manual method)

1. String Concatenation Using strcat() Function in C

The strcat() function is defined in the <string.h> header file.
It appends the second string to the first string.

Syntax:

strcat(destination, source);

Example Program:

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

int main() {
    char str1[50] = "Hello ";
    char str2[] = "World";

    strcat(str1, str2);

    printf("Concatenated String: %s", str1);
    return 0;
}

Output:

Concatenated String: Hello World

Important Notes:

  • Destination string must have enough space
  • strcat() does not check buffer overflow

2. String Concatenation Using strncat() Function in C

The strncat() function concatenates only a specified number of characters, making it safer.

Syntax:

strncat(destination, source, n);

Example Program:

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

int main() {
    char str1[50] = "Hello ";
    char str2[] = "World";

    strncat(str1, str2, 3);

    printf("Concatenated String: %s", str1);
    return 0;
}

Output:

Concatenated String: Hello Wor

Advantage:

  • Prevents buffer overflow
  • Safer than strcat()

3. String Concatenation Without Using Library Function in C

In interviews, you are often asked to concatenate strings without using strcat().

Logic:

  • Find the length of the first string
  • Append characters of the second string one by one

Example Program:

#include <stdio.h>

int main() {
    char str1[100] = "Hello ";
    char str2[] = "World";
    int i, j;

    // Find end of str1
    for(i = 0; str1[i] != '\0'; i++);

    // Append str2 to str1
    for(j = 0; str2[j] != '\0'; j++) {
        str1[i] = str2[j];
        i++;
    }

    str1[i] = '\0';

    printf("Concatenated String: %s", str1);
    return 0;
}

Output:

Concatenated String: Hello World

Comparison of String Concatenation Methods

MethodUses LibrarySafeInterview Friendly
strcat()Yes❌ No⚠️ Sometimes
strncat()Yes✅ Yes⚠️ Sometimes
Manual MethodNo✅ Yes✅ Highly

Common Mistakes in String Concatenation in C

  • Not allocating enough memory for destination string
  • Forgetting the null character ('\0')
  • Using strcat() on uninitialized arrays
  • Buffer overflow issues

Interview Questions on String Concatenation in C

  1. What is string concatenation in C?
  2. Difference between strcat() and strncat()
  3. Write a C program to concatenate two strings without library functions
  4. Why is strncat() safer than strcat()?

Real-World Use Cases

  • Joining user inputs
  • File path construction
  • Logging and message formatting
  • Data processing in embedded systems

Conclusion

String concatenation in C is a fundamental yet powerful concept. Whether you use strcat(), strncat(), or a manual approach, understanding how strings work internally in C is crucial for writing efficient and safe programs.

For interviews, always prefer the manual method to demonstrate strong logic and memory handling skills.

Read More

1 thought on “String Concatenation in C”

Leave a Comment