I wrote a program to convert float to it's binary IEEE754 representation in C.
Any improvements to this is appreciated
#include <stdio.h>
#include <stdlib.h>
void binary(float num) {
int n = *(int *) # //Evil bit hack from Quake3 Q_sqrt function
int size = sizeof(n) * 8;
char *s = malloc(1 + size); //A string which can hold 32 characters
s[size] = '\0';
int index = size - 1;
while(index >= 0) {
if (n & 1)
*(s + index) = '1';
else
*(s + index) = '0';
n >>= 1;
index--;
}
printf("%s\n", s);
free(s);
}
int main(void) {
float num;
scanf("%f", &num);
binary(num);
}
floator "convert float to it's binary IEEE754" (even whenfloatis not encoded as IEEE754)? \$\endgroup\$