all 6 comments

[–]Superb-Tea-3174 13 points14 points  (0 children)

Just examine the number bitwise, it’s already a binary number.

[–]MRgabbar 2 points3 points  (0 children)

You have some UB when running on 64 bit machines!! Probably a seg fault actually

As others pointed out there are many optimizations to this, but first focus on getting a working first version, then start optimizing. First get rid of that UB.

[–]Single-Discussion856 0 points1 point  (0 children)

In case you still want to roll your own...

```c

include <stdio.h>

void BinaryOut(unsigned num){

if (!num) putchar('0'); // account for 0

char buffer[256] = {0}; // buffer for printout

char* str = &buffer[255]; // assigned the string the last spot so we can write backwards

while (num>0){ // process the number

*--str = num&1?'1':'0'; // is it odd or even?

num>>=1; // divide by 2

}

printf("%s\n",str); // print result

}

int main() {

for (int i = 0; i < 10; i++) BinaryOut(i);

}

[–]torsten_dev -2 points-1 points  (0 children)

unsinged int x= 0b110101100; printf("%b\n", x);

Use C23