title: Converting an integer to binary in C
body: <p>I'm trying to convert an integer 10 into the binary number 1010. </p>
  
  <p>This code attempts it, but I get a segfault on the strcat():</p>
  
  <pre><code>int int_to_bin(int k)
  {
     char *bin;
  
     bin = (char *)malloc(sizeof(char));
     while(k&gt;0) {
        strcat(bin, k%2);
        k = k/2;
        bin = (char *)realloc(bin, sizeof(char) * (sizeof(bin)+1));
     }
     bin[sizeof(bin)-1] = '\0';
  
     return atoi(bin);
  }
  </code></pre>
  
  <p>How do I convert an integer to binary in C?</p>