Concatenating strings in C, which method is more efficient?

后端 未结 10 885
迷失自我
迷失自我 2020-11-28 19:51

I came across these two methods to concatenate strings:

Common part:

char* first= \"First\";
char* second = \"Second\";
char* both = malloc(strlen(fi         


        
10条回答
  •  有刺的猬
    2020-11-28 20:31

    • strcpy and strcat are much simpler oprations compared to sprintf, which needs to parse the format string
    • strcpy and strcat are small so they will generally be inlined by the compilers, saving even one more extra function call overhead. For example, in llvm strcat will be inlined using a strlen to find copy starting position, followed by a simple store instruction

提交回复
热议问题