C does not have the support for strings that some other languages have. A string in C is just a pointer to an array of char that is terminated by the first null character. There is no string concatenation operator in C.

Use strcat to concatenate two strings. You could use the following function to do it:

#include <stdlib.h>
#include <string.h>

char* concat(const char *s1, const char *s2)
{
    char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

This is not the fastest way to do this, but you shouldn't be worrying about that now. Note that the function returns a block of heap allocated memory to the caller and passes on ownership of that memory. It is the responsibility of the caller to free the memory when it is no longer needed.

Call the function like this:

char* s = concat("derp", "herp");
// do things with s
free(s); // deallocate the string

If you did happen to be bothered by performance then you would want to avoid repeatedly scanning the input buffers looking for the null-terminator.

char* concat(const char *s1, const char *s2)
{
    const size_t len1 = strlen(s1);
    const size_t len2 = strlen(s2);
    char *result = malloc(len1 + len2 + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here
    memcpy(result, s1, len1);
    memcpy(result + len1, s2, len2 + 1); // +1 to copy the null-terminator
    return result;
}

If you are planning to do a lot of work with strings then you may be better off using a different language that has first class support for strings.

Answer from David Heffernan on Stack Overflow
Top answer
1 of 12
236

C does not have the support for strings that some other languages have. A string in C is just a pointer to an array of char that is terminated by the first null character. There is no string concatenation operator in C.

Use strcat to concatenate two strings. You could use the following function to do it:

#include <stdlib.h>
#include <string.h>

char* concat(const char *s1, const char *s2)
{
    char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

This is not the fastest way to do this, but you shouldn't be worrying about that now. Note that the function returns a block of heap allocated memory to the caller and passes on ownership of that memory. It is the responsibility of the caller to free the memory when it is no longer needed.

Call the function like this:

char* s = concat("derp", "herp");
// do things with s
free(s); // deallocate the string

If you did happen to be bothered by performance then you would want to avoid repeatedly scanning the input buffers looking for the null-terminator.

char* concat(const char *s1, const char *s2)
{
    const size_t len1 = strlen(s1);
    const size_t len2 = strlen(s2);
    char *result = malloc(len1 + len2 + 1); // +1 for the null-terminator
    // in real code you would check for errors in malloc here
    memcpy(result, s1, len1);
    memcpy(result + len1, s2, len2 + 1); // +1 to copy the null-terminator
    return result;
}

If you are planning to do a lot of work with strings then you may be better off using a different language that has first class support for strings.

2 of 12
28

I'll assume you need it for one-off things. I'll assume you're a PC developer.

Use the Stack, Luke. Use it everywhere. Don't use malloc / free for small allocations, ever.

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

#define STR_SIZE 10000

int main()
{
  char s1[] = "oppa";
  char s2[] = "gangnam";
  char s3[] = "style";

  {
    char result[STR_SIZE] = {0};
    snprintf(result, sizeof(result), "%s %s %s", s1, s2, s3);
    printf("%s\n", result);
  }
}

If 10 KB per string won't be enough, add a zero to the size and don't bother, - they'll release their stack memory at the end of the scopes anyway.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ concatenating-two-strings-in-c
Concatenating Two Strings in C - GeeksforGeeks
The sprintf() function is used to print / store a formatted string to a string buffer. It can be used for concatenating two strings by using the source string as formatted string and destination string as string buffer.
Published: July 12, 2025
Discussions

Best way to concatenate a string in C?
strncpy should take 3 parameters, not sure where you are getting this version from. The last param is the max number of characters to copy. Also you should use strncat, not strcat. More on reddit.com
๐ŸŒ r/C_Programming
13
2
December 2, 2021
How can I make this string concat faster?
use malloc instead of calloc, If I remember correctly malloc just takes the memory, calloc makes each memory equal to zero. (I'm not sure coz I haven't used calloc). You can assign first char to '\0' from the memory from malloc, hence save some time there. Use a buffer instead, and keep track of actual allocated size and string length if you are going to concatenate to the same string over and over again. It'll reduce the number of realloc you have to do, (as if the allocated length is enough you can just use strcat()). Use multiple of 2^n length of the buffer, and depending upon the size of the strings you can decide the actual size, only realloc when your allocated length is not enough. Something like: int alloc_len = 1024; char *buffer = malloc(sizeof(char) * alloc_len); *buffer = '\0'; int str_len = 0; You can also put these in a struct and make your own string data type. Now to append to it: int piece_len = strlen(piece); if (str_len + piece_len >= alloc_len){ alloc_len += 1024; *buffer = realloc(buffer, sizeof(char) * alloc_len); } strcat(buffer, piece); str_len += piece_len; EDIT: I just remembered you don't even have to call malloc, you can call realloc with first argument NULL and it'll malloc for you. reducing one if condition in your program. More on reddit.com
๐ŸŒ r/C_Programming
16
2
February 18, 2021
How do I concatenate two strings in C?
What does that print? And why do you use strcpy? More on reddit.com
๐ŸŒ r/C_Programming
24
0
February 4, 2019
How to concatenate strings in R?
paste0("The mean of sample ", i, " is ", xbar5[i], ".") More on reddit.com
๐ŸŒ r/rstats
6
0
January 28, 2019
People also ask

Can I concatenate two strings in C without using strcat()?
Yes, you can concatenate two strings in C without using strcat() by manually copying the characters of both strings into a result array using loops or pointers.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
What is string concatenation in C programming?
String concatenation in C programming refers to combining two strings into one by appending the characters of one string to the end of another, ensuring the result is properly null-terminated.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
Can string concatenation modify the original string?
Yes, concatenation usually modifies the destination string by adding the second string at its end, so the original content changes.
๐ŸŒ
wscubetech.com
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how do i concatenate two strings in c?
r/C_Programming on Reddit: How do I concatenate two strings in C?
February 4, 2019 -

I want to concatenate two strings like hello world and goog morning in this way:hello worldgoog morning

#include <stdio.h>

#include <string.h>

int main ()

{

char a\[100\];

char b\[100\];

fgets(a,100,stdin);

fgets(b,100,stdin);

printf("%s",strcat(a,b));

}

Why doesn't this work? How should I do it?

๐ŸŒ
Programiz
programiz.com โ€บ c-programming โ€บ examples โ€บ concatenate-string
C Program to Concatenate Two Strings | Programiz
July 3, 2020 - However, in this example, we will concatenate two strings manually. #include <stdio.h> int main() { char s1[100] = "programming ", s2[] = "is awesome"; int length, j; // store length of s1 in the length variable length = 0; while (s1[length] != '\0') { ++length; } // concatenate s2 to s1 for (j = 0; s2[j] != '\0'; ++j, ++length) { s1[length] = s2[j]; } // terminating the s1 string s1[length] = '\0'; printf("After concatenation: "); puts(s1); return 0; }
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ concatenate-string-c
How to concatenate strings in C: A five-minute guide
A string variable directs to a character array stored in memory, and can be used to store a string value. To concatenate two string variables, you can use the strcat() function (more on that function later), which appends the second string to ...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ strings concatenation in c
Strings Concatenation in C - Scaler Topics
April 11, 2024 - String concatenation in C can be simply defined as the addition of two strings or updating them. In C language we can add two or more strings end to end or can update them using the concatenation process. The concatenation helps to combine two or more strings together and provide them as a single string value.
Find elsewhere
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ how to concatenate two strings in c
How to concatenate two strings in C - Aticleworld
September 21, 2022 - You can use the strcat function to concatenate two strings in C. It appends a copy of the string to the end of the destination string (including the terminating null character).
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-program-concatenate-strings
String Concatenation in C - Sanfoundry
November 10, 2023 - Methods used: char *str_concat(char*, ... using Function 1. Take the two strings as input. 2. Call the function str_concat to concatenate the second string to the first string....
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ c-programming โ€บ programs โ€บ concatenate-two-strings
C Program to Concatenate Two Strings (5 Ways)
September 17, 2026 - Learn 5 different ways to concatenate two strings in C with examples and step-by-step explanations. Master string manipulation techniques efficiently!
๐ŸŒ
StudyMite
studymite.com โ€บ c-programming-language โ€บ examples โ€บ concatenating-strings-in-c
Program to Concatenate Strings in C
2 weeks ago - Iterate through str2 character-by-character and append each character to the end of str1. Manually append the null-terminator (\0) to seal the concatenated string.
๐ŸŒ
W3Schools
w3schools.com โ€บ cs โ€บ cs_strings_concat.php
C# String Concatenation
You can also use the string.Concat() method to concatenate two strings: string firstName = "John "; string lastName = "Doe"; string name = string.Concat(firstName, lastName); Console.WriteLine(name); ... C# uses the + operator for both addition ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-concatenate-two-strings-without-using-strcat
C Program to Concatenate Two Strings Without Using strcat - GeeksforGeeks
July 23, 2025 - In this method, we use pointer arithmetic to traverse and concatenate the two strings. First, move the pointer to the end of the first string, then copy each character from second string one by one.
๐ŸŒ
DEV Community
dev.to โ€บ educative โ€บ how-to-concatenate-strings-in-c-a-five-minute-guide-o4k
How to concatenate strings in C: a five minute guide - DEV Community
December 21, 2020 - Here are few built-in functions that are available in the string.h header file: ... In C, the strcat() function is used to concatenate two strings. It concatenates one string (the source) to the end of another string (the destination).
๐ŸŒ
W3schools
w3schools.in โ€บ c-programming โ€บ examples โ€บ concatenate-two-strings-using-strcat
C Program to Concatenate Two Strings Using strcat - W3schools
#include <stdio.h> #include <string.h> ... return 0; } ... This program is used to concatenate two given strings as a single set of strings using the function strcat()....
๐ŸŒ
Hero Vired
herovired.com โ€บ learning-hub โ€บ topics โ€บ string-concatenation-in-c
String Concatenation in C : Using strcat() Function
In C language, Appending adds one string to another using the strcat() function. This function concatenates the contents of two strings by attaching the source strings to the end of the destination string to store the result..
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ best way to concatenate a string in c?
r/C_Programming on Reddit: Best way to concatenate a string in C?
December 2, 2021 -

I don't know why this was flagged for not being C previously this is C code. I'm trying to concatenate a two strings in C. Any idea how to do that? Here's what I'm trying to do but I get a warning. Is there any other way to do this?

     char i_20[20];
     char dest[20];
     
     strncpy(i_20, "jsldf");
     strncpy(dest, "./");
     
    strcat(dest, i_20);
    if (!_mkdir(dest)) {
        printf("Directory created \n");
    }
    else {
        printf("Unable to create directory");
    }
๐ŸŒ
Vultr
docs.vultr.com โ€บ examples โ€บ concatenate two strings
C Program to Concatenate Two Strings | Vultr Docs
December 11, 2024 - By using pointers, the function strcat_pointer moves to the end of dest and then copies src over. This eliminates the need for managing indices manually. Concatenating strings in C can be accomplished through several methods, each suitable for different situations.