Save bytes in float variable

You cannot do this, the %X format specifier expects an unsigned int, not a float (which is promoted to double when passed to a variadic function).
The compiler should have warned you about that:

<source>:5:30: warning: format '%X' expects argument of type 'unsigned int', but argument 2 has type 'double' [-Wformat=]
    5 |     printf("; lat_dist: 0x%08X ; ", Lat_delta_distance);
      |                           ~~~^      ~~~~~~~~~~~~~~~~~~
      |                              |      |
      |                              |      double
      |                              unsigned int
      |                           %08f

Unions won't solve this issue. Please stop teaching invalid C++ to beginners.

As mentioned earlier, you are not allowed to use type casts or unions for type punning. The only legal way is to use memcpy.

1 Like