Machine independent C library for printing the binary representation of data types to stdout I'm curious as to peoples thoughts on this small library I have made. I noticed there are no functions in the C standard library to accomplish this task. 

I have tried to make the code machine independent. If anyone has access to machines where short != 2 byte, int != 4 byte, long != 8 byte or where bytes themselves are not 8 bits. I would be curious as to the test results.

GitHub

The current functions for printing the binary representations are all slight modifications of the below:

#include &lt;limits.h&gt;
#include &lt;stdio.h&gt;

void printcb(char n)
{
    int i;
    int type_bits = CHAR_BIT;
    int mask = 1 &lt;&lt; type_bits - 1;

    for (i = 0; i &lt; type_bits; ++i) {
        if (n &amp; mask)
            putc('1', stdout);
        else
            putc('0', stdout);
        n &lt;&lt;= 1;
    }
    putc('\n', stdout);
}
You can just call putchar(), which automatically prints to stdout:

putchar('1');

The conditionals can instead be a ternary statement within putchar():

putchar(n &amp; mask ? '1' : '0');



putchar is often better suited for single-character output. putchar only has to pass a character directly to the output. This particular code isn't exactly performance-critical but it's useful to acquire good coding habits from the beginning.