title: Does printf() allocate memory in C?
body: <p>This simple method just creates an array of dynamic size n and initializes it with values 0 ... n-1. It contains a mistake, malloc() allocates just n instead of sizeof(int) * n bytes:</p>
  
  <pre><code>int *make_array(size_t n) {
      int *result = malloc(n);
  
      for (int i = 0; i &lt; n; ++i) {
          //printf("%d", i);
          result[i] = i;
      }
  
      return result;
  }
  
  int main() {
      int *result = make_array(8);
  
      for (int i = 0; i &lt; 8; ++i) {
          printf("%d ", result[i]);
      }
  
      free(result);
  }
  </code></pre>
  
  <p>When you check the output you will see that it will print some numbers as expected but the last ones are gibberish. However, once I inserted the printf() inside the loop, the output was strangely correct, even tho the allocation was still wrong!
  Is there some kind of memory allocation associated with printf()?</p>