-1
#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
    float f = 1.25; // set a breakpoint in the left!

    cout << " hex value=" << hex << f << endl;
    printf("%x", f);
    return 0;
}

With a breakpoint in that line, press F5 to debug, it shows the value is '-107374176.' , how does this number concluded? enter image description here

If I run without debug, the results in console is result in console

These two numbers also confused me a lot.

Animeta
  • 1,241
  • 3
  • 16
  • 30
  • 2
    It is some garbage value. `1.25` has not been assigned to `f` yet. Do one "Step Over" and then check `f`. And don't use inconsistent formats in `printf`. – Evg Sep 09 '18 at 10:02
  • %x is not for printing floats. [Using the wrong format specifier invokes UB](https://stackoverflow.com/q/16864552/995714) – phuclv Sep 09 '18 at 10:16

2 Answers2

1

Putting a breakpoint at that line does not mean that particular line has been evaluated / executed yet. Previous statements (above the breakpoint) have been executed, but that one has yet to be executed. Variable f has not been initialized at that point so you are seeing some random garbage in memory. Press F10 to step over that line and observe the different results.

One possible workflow could be:

  • Press F9 to set a breakpoint in your code
  • Press F5 to start debugging
  • Keep pressing F10 to step through your code one line at the time
Ron
  • 14,674
  • 4
  • 34
  • 47
0

Thanks to @Evg and @ phuclv, I know how the number '-107374176.' appears.

With that breakpoint, 1.25 is not assigned to f, then the value of f is (cccccccc) in hex,

(cccccccc)hex = (‭1 10011001 10011001100110011001100‬)bin

(10011001)bin = (153)dec, 153 - 127 = 26

(1.10011001100110011001100) = (1.5999999046325684)dec

so (‭1 10011001 10011001100110011001100‬)bin = (-1.5999999046325684 * 2^26)dec

= (107374176.0000000027262976)dec

Animeta
  • 1,241
  • 3
  • 16
  • 30