简体   繁体   中英

scanf floating numbers doesn't work with %d

float a = 0.0, b = 0.0;

When I do

scanf("%f %f", &a, &b);  

it works but

scanf("%d %d", &a, &b);

doesn't work since a and b are not given the scanned values. Why is that?

%d is for decimal integer numbers so you should declare a and b as int.

You are experiencing undefined behavior. Although it is utterly pointless to speculate about undefined behavior, probably what is happening is that the bit values corresponding the the integer given on the input stream are put into the variables, but when interpreted as floats they give different values. That is to say that 0x00000001 may represent the value 1 when interpreted as an integer, that bit sequence has a different value when interpreted as a float.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM