title: print a integer in c using putchar only
body: <p>This is an old practice and I am trying to identify where i went wrong with my code: write a c program to print an integer using putchar only. I know one right way to do it is:</p>
  
  <pre><code>void printnumber(int n)
  {
  
      if (n &lt; 0) {
          putchar('-');
          n = -n;
      }
  
      if (n == 0)
          putchar('0');
  
      if (n/10)
          printnumber(n/10);
  
      putchar(n%10 + '0');
  }
  </code></pre>
  
  <p>I just want to know why my way of doing it does not work, though as I was trying to debug using step over, it looks there is no problem with my procedure, however, the code is printing some funny character. I thought it is because putchar() read the number as ascii value and print the character corresponding to ascii value, and maybe this is why in the code above, we are using <code>putchar(n%10+'0')</code>, so I tried to add <code>'0'</code> to all my putchar code, but it does not work properly. So here is my code and result without and with <code>'0'</code> when i=-123</p>
  
  <pre><code>void printnumber(int i)
  {
  
      if(i/10!=0)
      {
          putchar(i%10);
          printnumber((i-i%10)/10);
      }
      else if((i/10==0) &amp;&amp; (i%10!=0) &amp;&amp; (i&gt;0))
          putchar(i%10);
      else if((i/10==0) &amp;&amp; (i%10!=0) &amp;&amp; (i&lt;=0))
          putchar(-i%10);
  
  }
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/y4T8X.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/WmQhV.png" alt="enter image description here"></p>