91

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 code which I need working in c!

  String prefix = "pre_";
  String suffix = "_suff";

  // This is the string I need formed:
  //  e.g. "pre_3_suff"
  String result = prefix + i + suffix;
}

I tried using various combinations of strcat and itoa with no luck.

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
john
  • 2,733
  • 6
  • 23
  • 14
  • 4
    show us what you've tried instead of what you want, you'll learn much more with comments on your code than people telling you what to do – CharlesB Mar 02 '11 at 19:09
  • State your problem in full... it seems you are having a problem but which problem you didn't mentioned? the string is not forming or what? – S M Kamran Mar 02 '11 at 19:11
  • 10
    @SMKamran: This is not his code. It's Java-style pseudocode. His problem is that he doesn't know how to do this in C. – Lightness Races in Orbit Mar 02 '11 at 19:12

3 Answers3

149

Strings are hard work in C.

#include <stdio.h>

int main()
{
   int i;
   char buf[12];

   for (i = 0; i < 100; i++) {
      snprintf(buf, 12, "pre_%d_suff", i); // puts string into buffer
      printf("%s\n", buf); // outputs so you can see it
   }
}

The 12 is enough bytes to store the text "pre_", the text "_suff", a string of up to two characters ("99") and the NULL terminator that goes on the end of C string buffers.

This will tell you how to use snprintf, but I suggest a good C book!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    I want to give +1, but the first line of your answer rather contradicts the rest of it. You've shown that in fact the solution is easy; you just have to throw out the notion that inefficient string concatenation idioms from script languages translate over to C. `snprintf` is the answer to almost any C string-assembly question. – R.. GitHub STOP HELPING ICE Mar 02 '11 at 22:06
  • 5
    @R. What I've shown is that the solution is not quite as easy as the OP was hoping. "Just" throwing out that notion is harder for some than for others. – Lightness Races in Orbit Mar 02 '11 at 22:10
  • 1
    Also you should be using `snprintf`, not `sprintf`. I missed that on the first read. Your code is very dangerous as written, since if the 100 is changed without updating the buffer size, you'll clobber the stack. – R.. GitHub STOP HELPING ICE Mar 02 '11 at 22:11
  • 2
    @R. When you change the 100, you change the buffer size. Using `snprintf` does not change that; it just means that you have one more place to write and update the buffer size. – Lightness Races in Orbit Mar 02 '11 at 22:12
  • 6
    The difference is that your code crashes (or worse yet yields a privilege compromise) when somebody forgets to change the buffer size, while the version with `snprintf` just truncates the string. In any case I would make the buffer size `12+3*sizeof(int)` and then you don't have to worry... but it would still be better to use `snprintf`. – R.. GitHub STOP HELPING ICE Mar 02 '11 at 22:44
  • @delive: What do you mean by "not work"? What actually happened for you? – Lightness Races in Orbit Nov 01 '16 at 12:02
9

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");
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
1

Look at snprintf or, if GNU extensions are OK, asprintf (which will allocate memory for you).

vanza
  • 9,715
  • 2
  • 31
  • 34
  • 3
    Allocating the memory "for you" is hardly doing you a favor. There's a small constant bound on the size needed, so it makes much more sense to provide the buffer yourself. With `asprintf` you'd have to add a test for allocation failure and code to free the buffer later. – R.. GitHub STOP HELPING ICE Mar 02 '11 at 22:08