C language to achieve the combination of strings


After learning Perl and python, processing strings is just something I like to do. String splicing is an easy thing in these high-level scripting languages.

C language is my entry-level programming language, but I haven’t always understood this language very well. To be exact, I know little about the huge standard library and GNU and other function libraries.

How to use C language to splice strings? Previously, I must define a new “string” and then copy it in memory. In fact, there is a twin brother of printf function that can realize this function, that is sprintf.

In fact, when I came into contact with the operating system, I had a little understanding of this function. That is, this function is lower than printf when implementing screen display, and can directly operate memory. So, how to use this function to splice strings?

The model code is as follows:





int main(void)


  char str1[] = "my string 1";

  char str2[] = "string 2";

  char *strCat = (char*)malloc(strlen(str1) + strlen(str2));






  return 0;


The code is compiled and executed as follows:



my string 1string2

From the above results, we can see that the string splicing function is realized through sprintf.

The above C language merging string is all that Xiaobian has shared with you. I hope it can give you a reference and support developeppaer.