title: Create char array from int in C
body: <p>How do you put an int into a char array?</p>
  
  <pre><code>int x = 21, i = 3;
  char length[4];
  
  while(i &gt;= 0) {
      length[i] = (char) (x % 10);
      x /= 10;
      i--;
  } printf("%s\n", length);
  
  // length should now be "0021"
  </code></pre>
  
  <p>The string comes out blank instead.</p>
  
  <p>Note: This is not a duplicate of "How do I convert from int to chars in C++?" because I also need padding. i.e. "0021" not "21"</p>