Convert number to X base As an educational exercise, I would like to make this code more efficient, with fewer lines, without using other standard functions.

The goal here is to convert a number to X base and display it, (sample for hexadecimal) including 0x0 => 0, or 0x4096 => 4096.

int     print_hex(int number, int base)
{
    int     i;
    int     len;
    char    c;
    char    buffer[base];

    if (!number)
    {
        putchar('0');
        return 1;
    }
    i = 0;
    while (i &lt; base &amp;&amp; number &gt; 0)
    {
        c = "0123456789abcdef"[number % base];
        buffer[i] = c;
        number /= base;
        i++;
    }
    buffer[i] = '\0';
    len = i;
    while (i--)
        putchar(buffer[i]);
    return (len);
}
Your code does not handle negative numbers.  As it is, it will print nothing.  The code should be changed to either handle the negatives properly, or take number as an unsigned where it is not an issue.

Your use of base as the length of the array to store the converted number in is incorrect.  A 32-bit integer will potentially need 32 characters to display the entire number, assuming no zero byte terminator (see below).  buffer should be declared as a fixed size, large enough to hold the longest possible string.  You could then eliminate the i &lt; base check from your while loop.

The initial check of if (!number) can be eliminated by changing your while loop into a do/while look.

If you're looking for brevity in code lines, the body of the loop can be reduced to two statements, although it is more readable with three or four.

The assignment of a 0 byte to the end of buffer is unnecessary in this context since you never pass buffer to a function that requires it.