title: writing formatted data of unknown length to a string (C programming)
body: <p>The following C function:</p>
  
  <pre><code>int sprintf ( char * str, const char * format, ... );
  </code></pre>
  
  <p>writes formatted data to a string. The size of the array passed as str should be enough to contain the entire formatted string. However, what if the length of the formatted string is not known ahead of time? How can this function (or another function like it) be used write formatted data which has a length which is unknown?</p>
  
  <p>For example:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main ()
  {
    char buffer [13];
    int n, a=5, b=3;
    n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
    printf ("[%s] is a %d char long string\n",buffer,n);
    return 0;
  }
  </code></pre>
  
  <p>The buffer needs to be 13 or greater in order for this to work. If the string length was unknown, and the buffer, for example has been set for 5, this would not work. I need something that can dynamically allocate or reallocate the buffer for strings that happen to be bigger than the buffer. </p>