My programming environment is gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)
I code like:
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len){
int i;
for (i = 0; i<len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
void show_float(float x){
show_bytes((byte_pointer)&x, sizeof(float));
}
int main(){
int y = 0xffffff;
float f = y;
show_float(f);
return 0;
}
and the machine give the result: 00 00 00 e0
I think it is not right according to IEEE 754; but i don't know why.
while the same code in VS 2013 in windows give the right answer: ff ff 7f 4b
Does gcc 5.4.0 not adopt the IEEE 754? Or are there some problem in my code?
Does gcc 5.4.0 not adopt the IEEE 754?
Or are there some problem in my code?
gcc 5.4.0 and IEEE 754 is not the issue. Certainly the code is not conforming
By re-ordering the functions, yet same code, I get 2 warnings and can replicate OP's output 00 00 00 e0
warning: implicit declaration of function 'show_float' [-Wimplicit-function-declaration]
warning: conflicting types for 'show_float'
I suspect OP has not posted true code. - or its not all in one file. The real code has the usually problem of code passing double
- due to lack of a prior declaration/definitioon, yet show_float()
expects a float
.
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len){
int i;
for (i = 0; i<len; i++)
printf(" %.2x", start[i]);
printf("\n");
}
int main(){
int y = 0xffffff;
float f = y;
show_float(f); // code lacks proto-type, so assumes it needs to pass a double
return 0;
}
void show_float(float x){
show_bytes((byte_pointer)&x, sizeof(float));
}
Fix by declaring prototypes or re-order code.
#include <stdio.h>
typedef unsigned char *byte_pointer;
void show_bytes(byte_pointer start, int len);
void show_float(float x);
/* the 3 functions in any order */