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

String Concatenation: Safety and Performance

Understanding concatenation in C means understanding more than strcat(). C strings are null-terminated character arrays, so the destination buffer must have enough capacity for the combined text plus the terminating \\0 character.

Why strcat() Can Be Dangerous

strcat() does not know the allocated size of the destination array. If the destination buffer is too small, the program can write beyond its boundary. That can cause memory corruption and undefined behavior.

A Safer Development Pattern

When building a string dynamically, track the destination capacity and the current length. Prefer APIs or helper functions that accept buffer sizes when available, and validate every input length before copying data.

Concatenation Without strcat()

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

int main(void) {
    char first[40] = "GangForCode";
    const char second[] = " - C Tutorial";

    size_t used = strlen(first);
    size_t available = sizeof(first) - used - 1;

    if (strlen(second) <= available) {
        memcpy(first + used, second, strlen(second) + 1);
    }

    printf("%s\n", first);
    return 0;
}

This example explicitly checks the remaining buffer capacity before copying the second string and preserves the terminating null character.

Complexity

Most straightforward concatenation techniques must inspect the existing destination string to locate its terminator. When many strings are repeatedly appended to the same buffer, repeatedly scanning the destination can become inefficient. Tracking the current length can avoid unnecessary scans.

Interview Checklist

  • Explain why C does not have a built-in string type.
  • Explain the role of the null terminator.
  • Explain the preconditions of strcat().
  • Write concatenation logic without library functions.
  • Discuss buffer size, overflow, and safe copying.

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

3 thoughts on “String Concatenation in C”

Leave a Comment