The write function does not print a floating point number in the following code:
#include <unistd.h>
int main(){
float f = 4.5;
write(1,&f,sizeof float);
return 0;
}
This results in:
�@
Whereas:
int main(){
char *c = "Hello world";
write (1,c,strlen(c)+1);
return 0;
}
Prints Hello world as expected.
What am I missing?
Thanks in advance.
writeoutputs the bytes in the binary representation of the floating-point number. Those bytes do not even always correspond to readable characters, let alone to the textual representation of the number itself.I guess you want to convert the number to human-readable text. That’s what
printfis for: