As an aside, I find bit-shifting a cleaner method for converting integers to binary this way:
Code:
itsme@itsme:~/C$ cat inttobin.c
#include <stdio.h>
char *inttobin(int num)
{
// Make sure binrep can hold all the binary digits plus 1 for the '\0'
static char binrep[sizeof(int) * 8 + 1];
int bit;
int index;
// Loop through num from MSB to LSB
for(index = 0, bit = sizeof(int) * 8 - 1;bit >= 0;--bit)
binrep[index++] = ((num >> bit) & 1) + '0';
// Be sure to terminate the string
binrep[index] = '\0';
// Skip all the leading '0's
for(index = 0;binrep[index] == '0';++index)
;
// Make sure we have at least one digit to show
if(!binrep[index])
index--;
return binrep + index;
}
int main(void)
{
printf("%s\n", inttobin(1234567));
return 0;
}
Code:
itsme@itsme:~/C$ ./inttobin
100101101011010000111