cprintfbinaryformatter

How I can divide my output (binary #) into two groups separated by a single space in C (Without Arrays)


I'm having difficulty formatting my output binary number in C Language. I'm trying to divide the output number into two groups of 8 digits, which would be separated by a single space.

this is my printf:

printf("%016ld in Binary\n", decNumtoBin);

This is my the output 0000001000101011

I wanted to look like this 00000010 00101011


Solution

  • Posting as 'answer', as requested in comments. In order to understand why it works, remember, you aren't actually printing a binary number, but a decimal one. And all you want to do is split it into 2 parts, which can be easily done using a simple division and a modulo.

    printf("%08ld %08ld in Binary\n", decNumtoBin/100000000, decNumtoBin%100000000);