How to concatenate string and int in C?

前端 未结 3 360
我在风中等你
我在风中等你 2020-12-07 17:27

I need to form a string, inside each iteration of the loop, which contains the loop index i:

for(i=0;i<100;i++) {
  // Shown in java-like cod         


        
3条回答
  •  忘掉有多难
    2020-12-07 18:16

    Use sprintf (or snprintf if like me you can't count) with format string "pre_%d_suff".

    For what it's worth, with itoa/strcat you could do:

    char dst[12] = "pre_";
    itoa(i, dst+4, 10);
    strcat(dst, "_suff");
    

提交回复
热议问题