Convert decimal to binary This program converts a decimal number to a binary number. This is one of my first C programs and I am wondering if I have used the elements of this language properly. Suggestions for improvement are welcome.

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

void print_out_reversed(char string[])
{
    int index = strlen(string);

    while (string[index] != '\0')
        index--;

    for (int i = index; i &gt;= 0; i--)
        putchar(string[i]);
    putchar('\n');
}

void print_decimal_number_binary(int number)
{
    if (number == 0)
    {
        printf("0\n");
        return;
    }

    char bits[sizeof(int) * 8 + 1] = {0};
    int index = 0;

    while (number &gt; 0)
    {
        if (number % 2 == 0)
        {
            bits[index] = '0';
        }
        else
        {
            bits[index] = '1';
        }
        number = number / 2;
        index++;
    }

    print_out_reversed(bits);
}

int main()
{
    printf("enter number: ");
    int number;
    scanf("%i", &amp;number);
    print_decimal_number_binary(number);
}
Terminology

It's important to be able to understand (and describe) what's actually going on. Your program


converts from an integer decimal string representation to an integer using scanf. This integer is then represented as a binary number in the processor.
converts from that integer back into a string representation, but rather than it being decimal, it's binary.


So yes - it technically converts from "decimal to binary", but really it's "decimal string to integer to binary string".

Use const

void print_out_reversed(char string[])


doesn't modify string, so write const char string[].

Simplify your strlen usage

This:

int index = strlen(string);

while (string[index] != '\0')
    index--;

for (int i = index; i &gt;= 0; i--)
    putchar(string[i]);


can be

for (int i = strlen(string)-1; i &gt;= 0; i--)
    putchar(string[i]);


It seems that you don't trust what strlen is doing, which is why you have that intermediate while loop. But that loop won't have any effect, because the null terminator will always be where strlen says it is.

Use math instead of if

This:

    if (number % 2 == 0)
    {
        bits[index] = '0';
    }
    else
    {
        bits[index] = '1';
    }


can be

bits[index] = '0' + (number &amp; 1);


Use combined operation and assignment

This:

number = number / 2;


should be

number /= 2;


or, for speed (which the compiler will do for you anyway)

number &gt;&gt;= 1;