In C, "strings" are just plain char arrays. Therefore, you can't directly concatenate them with other "strings".

You can use the strcat function, which appends the string pointed to by src to the end of the string pointed to by dest:

char *strcat(char *dest, const char *src);

Here is an example from cplusplus.com:

char str[80];
strcpy(str, "these ");
strcat(str, "strings ");
strcat(str, "are ");
strcat(str, "concatenated.");

For the first parameter, you need to provide the destination buffer itself. The destination buffer must be a char array buffer. E.g.: char buffer[1024];

Make sure that the first parameter has enough space to store what you're trying to copy into it. If available to you, it is safer to use functions like: strcpy_s and strcat_s where you explicitly have to specify the size of the destination buffer.

Note: A string literal cannot be used as a buffer, since it is a constant. Thus, you always have to allocate a char array for the buffer.

The return value of strcat can simply be ignored, it merely returns the same pointer as was passed in as the first argument. It is there for convenience, and allows you to chain the calls into one line of code:

strcat(strcat(str, foo), bar);

So your problem could be solved as follows:

char *foo = "foo";
char *bar = "bar";
char str[80];
strcpy(str, "TEXT ");
strcat(str, foo);
strcat(str, bar);
Answer from Brian R. Bondy on Stack Overflow
Top answer
1 of 16
499

In C, "strings" are just plain char arrays. Therefore, you can't directly concatenate them with other "strings".

You can use the strcat function, which appends the string pointed to by src to the end of the string pointed to by dest:

char *strcat(char *dest, const char *src);

Here is an example from cplusplus.com:

char str[80];
strcpy(str, "these ");
strcat(str, "strings ");
strcat(str, "are ");
strcat(str, "concatenated.");

For the first parameter, you need to provide the destination buffer itself. The destination buffer must be a char array buffer. E.g.: char buffer[1024];

Make sure that the first parameter has enough space to store what you're trying to copy into it. If available to you, it is safer to use functions like: strcpy_s and strcat_s where you explicitly have to specify the size of the destination buffer.

Note: A string literal cannot be used as a buffer, since it is a constant. Thus, you always have to allocate a char array for the buffer.

The return value of strcat can simply be ignored, it merely returns the same pointer as was passed in as the first argument. It is there for convenience, and allows you to chain the calls into one line of code:

strcat(strcat(str, foo), bar);

So your problem could be solved as follows:

char *foo = "foo";
char *bar = "bar";
char str[80];
strcpy(str, "TEXT ");
strcat(str, foo);
strcat(str, bar);
2 of 16
307

Avoid using strcat in C code. The cleanest and, most importantly, the safest way is to use snprintf:

char buf[256];
snprintf(buf, sizeof(buf), "%s%s%s%s", str1, str2, str3, str4);

Some commenters raised an issue that the number of arguments may not match the format string and the code will still compile, but most compilers already issue a warning if this is the case.

🌐
GeeksforGeeks
geeksforgeeks.org › c language › concatenating-two-strings-in-c
Concatenating Two Strings in C - GeeksforGeeks
The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example: ... #include <stdio.h> #include <string.h> int main() { char s1[] = "Hello "; char s2[] = "Geeks"; // Concatenate str2 ...
Published: July 12, 2025
Discussions

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
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
Code review: string concatenation in C
This is... pretty bad. Starting with a terrible coding standard (no empty lines in code? Not initializing variables when they are declared? Really?) and ending with a program that is functionally identical (save a few printfs) with the initial version but that now somehow has fixed the buffer overrun. And judging by the diagrams the "fix" was smushing the two arrays together somehow so they are now contiguous in memory. This is horrible. More on reddit.com
🌐 r/C_Programming
8
0
December 1, 2020
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
🌐
IBM
ibm.com › docs › en › i › 7.4.0
strcat() — Concatenate Strings
IBM web domains ibm.com, ibm.org, ibm-zcouncil.com, insights-on-business.com, jazz.net, mobilebusinessinsights.com, promontory.com, proveit.com, ptech.org, s81c.com, securityintelligence.com, skillsbuild.org, softlayer.com, storagecommunity.org, think-exchange.com, thoughtsoncloud.com, alphaevents.webcasts.com, ibm-cloud.github.io, ibmbigdatahub.com, bluemix.net, mybluemix.net, ibm.net, ibmcloud.com, galasa.dev, blueworkslive.com, swiss-quantum.ch, blueworkslive.com, cloudant.com, ibm.ie, ibm.fr, ibm.com.br, ibm.co, ibm.ca, community.watsonanalytics.com, datapower.com, skills.yourlearning.ibm.com, bluewolf.com, carbondesignsystem.com, openliberty.io
🌐
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?

🌐
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 ...
🌐
W3Schools
w3schools.com › cpp › cpp_strings_concat.asp
C++ String Concatenation
This is called concatenation: string firstName = "John "; string lastName = "Doe"; string fullName = firstName + lastName; cout << fullName; Try it Yourself » · In the example above, we added a space after firstName to create a space between John and Doe on output.
Find elsewhere
🌐
Red Hat
developers.redhat.com › blog › 2019 › 08 › 12 › efficient-string-copying-and-concatenation-in-c
Efficient string copying and concatenation in C | Red Hat Developer
March 28, 2023 - The idiomatic (though far from ideal) way to append two strings is by calling the strcpy and strcat functions as follows. ... To perform the concatenation, one pass over s1 and one pass over s2 is all that is necessary in addition to the ...
🌐
YouTube
youtube.com › watch
Concatenating strings in C - YouTube
strcat is a function many of us know. In this video we will not only look at how to use strcat to concatenate strings but also how to safely do so without ru...
Published: April 12, 2020
🌐
GNU
gcc.gnu.org › onlinedocs › cpp › Concatenation.html
Concatenation (The C Preprocessor)
It would be cleaner not to have to give each command name twice, once in the string constant and once in the function name. A macro which takes the name of a command as an argument can make this unnecessary. The string constant can be created with stringizing, and the function name by concatenating the argument with ‘_command’. Here is how it is done:
🌐
W3Schools
w3schools.com › cs › cs_strings_concat.php
C# String Concatenation
You can also use the string.Concat() method to concatenate two strings:
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › csharp › fundamentals › strings › common-tasks › concatenate
Concatenate strings in C# - C# | Microsoft Learn
June 26, 2026 - Learn how to combine strings in C# with the + operator, string interpolation, string.Concat, string.Join, and StringBuilder, and how to choose between them.
🌐
Quora
quora.com › Can-you-concatenate-strings-with-the-operator-in-the-C-programming-language-If-not-how-can-you-concatenate-strings-in-C
Can you concatenate strings with the + operator in the C programming language? If not, how can you concatenate strings in C? - Quora
Answer (1 of 2): Question: Can you concatenate strings with the + operator in the C programming language? If not, how can you concatenate strings in C? You clearly do not know any C programming so why are you starting with strings? Start with other things like basic input/output - something like...
🌐
Facebook
facebook.com › groups › cs50 › posts › 924719527675093
How to concatenate strings in C
Explore the things you love · Log into Facebook · English (US) · فارسی · العربية · Türkçe · Deutsch · Français (France) · Polski · More languages…
🌐
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; }
🌐
DigitalOcean
digitalocean.com › community › tutorials › string-concatenation-in-c-plus-plus
String Concatenation in C++: 4 Ways To Concatenate Strings | DigitalOcean
August 3, 2022 - Technical tutorials, Q&A, events — This is an inclusive place where developers can find or lend support and discover new ways to contribute to the community.
🌐
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");
    }
🌐
Codefinity
codefinity.com › courses › v2 › 5ce35233-a099-4fe9-b172-f52fe1e84d86 › c45926a2-ead1-47a2-9081-55730780fb3d › 2c31be7e-bac7-453f-b200-76b84867bfb0
Learn Concatenating Strings | Common String Operations
When working with strings in C, you often need to combine, or concatenate, two or more strings into one. This process is called string concatenation. C provides standard library functions for this purpose, with strcat and strncat being the most commonly used.
🌐
Wyzant
wyzant.com › resources › ask an expert
How do I concatenate const/literal strings in C? | Wyzant Ask An Expert
May 20, 2019 - I'm working in C, and I have to concatenate a few things.Right now I have this: message = strcat("TEXT ", var); message2 = strcat(strcat("TEXT ", foo), strcat(" TEXT ", bar));Now if you have experience in C I'm sure you realize that this gives you a segmentation fault when you try to run it. So how do I work around that? ... sprintf(outbuff, "%s : %s : %s : %s ","This is string #1","This is string #2","This is string #3","This is stirng #4");
🌐
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 - The sprintf() function in C can write a formatted string to a string buffer. It can be used to concatenate string by formatting second string at the end of the first string by treating it as a string buffer.
🌐
Educative
educative.io › blog › concatenate-string-c
How to concatenate strings in C: A five-minute guide
The sprintf() function formats data and writes the result into a string buffer. It can concatenate strings while also inserting variables, numbers, or formatted output. Unlike strcat(), sprintf() creates the final string directly instead of ...