title: I have written a C program to convert between decimal, hex and binary
body: <p>I cant seem to get hexadecimal portion right, I get the right output but the output keeps going on the screen and never stops until i hit ^z. Havent been able to test the rest of the code yet.<br />
  /* This program converts decimal to either binary
  * or hexadecimal
  */</p>
  <pre><code>#include&lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  /*function prints binary of decimal n*/
  void binary(unsigned n)
  {
  /* step 1 recurse dividing by 2*/
  if (n &gt; 1)
      bin(n / 2);
  
  /* step 2 print bit*/
  printf(&quot;%d&quot;, n % 2);
  }
  
  
  int main(int argc, char const *argv[])
  {
  char i;
  int c;
  /*    run till ^d entered
  */
  while ((i = scanf(&quot;%d&quot;, &amp;c)) != EOF) {
  
      /*        check for argument flag
      */
      if (strcmp(argv[1], &quot;-x&quot;) == 0)
      {
          /*convert to hexadecimal and print*/
          int decimal = c;
          /*read decimal as flag if ^d is not present in flag*/
          printf(&quot;%s&quot;, &quot;0x&quot; );
          /*starting 0x formatting for hex*/
          printf(&quot;%x&quot;, decimal);
          /*new line for next input*/
          printf(&quot;\n&quot;);
  
  
      } else if (strcmp(argv[1], &quot;-b&quot;) == 0)
      {
          /*            convert to binary and print
          */
          int decimal;
          /*read decimal as flag if ^d is not present in flag*/
  
          decimal = c;
          binary(decimal);
          /*new line for next input*/
          printf(&quot;\n&quot;);
      } else {
          /*            prompt help, incorrect flag argument
          */
          printf(&quot;%s\n&quot;, &quot;Usage: ./convert [-x |-b]&quot;);
          break;
      }
  
  
  }
  return 0;
  }
  </code></pre>