All you are actually doing is converting a number to binary.... A simple loop does this without any library calls (aside from printf
)...
const unsigned int numbits = 3;
unsigned int bit;
for( bit = 1U << (numbits-1); bit != 0; bit >>= 1 ) {
printf( number&bit ? "1" : "0" );
}
printf( "\n" );
Edited, since you seem to want recursion. You need to have some way to specify how many bits you require. You need to pass this into your recursive routine:
#include <stdio.h>
void permute(unsigned number, unsigned bits)
{
if( bits == 0 ) return;
permute(number / 2, bits-1);
printf( "%d", number % 2 );
} //permute ends here
void permuteN(unsigned number, unsigned bits ) {
unsigned i;
for(i = 0; i < number + 1; i++){
permute(i, bits);
printf("\n");
}
} //permuteN ends here
int main(void)
{
permuteN(7, 3);
return 0;
}
To get the output in the order you require, you can't know when to write the newline. So in this case, you write it afterwards.