title: C concatenate string with int in loop
body: <p>I'm new to C and am having trouble with strings.  What I would like to do is create a string like "val1, val2, val3" in a loop.</p>
  
  <p>Currently my code looks something like:</p>
  
  <pre><code>char tagstr[60] = "";
  int k;
  int n = 5;
  for (k=0; k &lt; n; k++) {
      char temp[10]  = "";
      sprintf(temp, ", val%d", k);
      strcat(tagstr, temp);
  }
  </code></pre>
  
  <p>But the output of tagstr is ", val#", where # is some long integer value.  I'm guessing I'm doing something wrong with pointers here but I've tried everything I can think of without success... any help would be much appreciated.</p>
  
  <p>EDIT: more context, if it helps:</p>
  
  <pre><code>int tagsClosed = strlen(pch1) - strcspn(pch1, ")");
  do {
      if (curTag.size &gt; 0) {
          // problem section
          char tagstr[60] = "";
          int k;
          for (k = 0; k &lt; 5; k++) {
              char temp[10] = "";
              sprintf(temp, ", val%i", temp, k);
              strcat(tagstr, temp);
          }
  
          // This prints out something like ", val-890132840" x 5 (same number)
          printf ("String is now: %s\n", tagstr);
      }
      curTag = *(curTag.parent);
      tagsClosed--;
  } while (tagsClosed &gt; 0);
  </code></pre>
  
  <p>curTag is a struct:</p>
  
  <pre><code>typedef struct Tag {
      char * name;
      int size; // number of children
      int tagnum;
      struct Tag* parent;
  } Tag;
  </code></pre>