I am trying to convert an int, a char, and a short into their binary values and display it.

int a;
char b;
short c;

I understand how to convert an integer to a binary value bit by bit, I just don't understand how to store it as a varible so I can print it out as a binary value in a printf() line.

int temp = a%2; //the value of the bit starting at the least significant
a = a/2;
//how would i store this bit by bit

The problem is to print the obtained remainders in the reverse order which implies that you'll have to store some sort of information abt what remainders you obtained in the previous step.

One way to do it is to use a character array as such: char convert = "01"; Then you can use the remainder obtained as the index to the above string and store the resulting character in another array for later display.
The advantage of the above logic is that you can easily extend your code to make it convert to octal or hexadecimal just by doing: char convert="0123456789ABCDEF" . There recently was a thread which had this program. You can take a look at that.

Another way is to have a multiplier to multiply your obtained remainder with.

code snippet:

        temp = num%2;      //obtain 0 or 1
        temp = mul * temp;// multiply with 1 or 10 or 100...
        sum+= temp;        // sum holds the final value
        mul*=10;              // modify mul and num for the next iteration
This article has been dead for over six months. Start a new discussion instead.