Given the following code snippet:
int *iptr; float *fptr; float fval; fval = 0.0; fptr = &fval; iptr = fptr; printf("%d \n", *iptr); fval = 1.0; printf("%d \n", *iptr);
Output:
01065353216
Why does the first print statement at least roughly match the value associated with * iptr (0.0), but the second print statement doesn't work?
When you write printf("%d \n", *iptr);, you are asking to printfinterpret the value that is indicated iptras an integer.
printf("%d \n", *iptr);
printf
iptr
It so happened that the floatversion 0is represented by the same version bits int 0. In particular, all bits 0.
float
0
int
float, 1.0, ( IEEE), int.
1.0
Wikipedia , float .
iptr = fptr; undefined.
iptr = fptr;
.
@merlin . , , . undefined. , .
iptr int * fptr float *. fptr to iptr - , , fval. . undefined, . , . , , undefined.
int *
fptr
float *
fval
undefined: iptr = fptr;. float int, IEEE754
:
printf("%d", fval);
A floating point number consists of zeros. Any other number is not just a number, but an exponent and a mantissa - of course, not one of them is your number.
Floats are stored in the IEEE754 format, in which the value of a number, such as 1.0, is very different from 1 (it is offset to allow negative numbers and numbers). You cannot print float as int and get a result similar to the number assigned to a floating point.
Source: https://habr.com/ru/post/1536060/More articles:What exactly is warning C4718 (Visual Studio)? - c ++Zip Compression in Go - goFile directory and PHP MVC startup problem - phpCross browser and audio with multiple devices - javascript(jqplot) How to show a small grid in the log scale graph? - javascriptFontawesome not working while servicing IIS - cssXcode 5 Slowdown when opening the "Connection Inspector" in an xib or storyboard file - xibiOS: MPVolumeView Game Collector - iosiOS AirPlay Action Sheet для публикации публичного магазина приложений - например, в Spotify - iosWhat is the code to directly call an AirPlay action sheet? - objective-cAll Articles