I get previous float value when I am printing new value

后端 未结 2 1613
孤独总比滥情好
孤独总比滥情好 2021-01-24 01:24

I am getting output 0.23 from second printf. But typecasting gives required output. If I am not using type casting previous value is printed. Compiler

2条回答
  •  無奈伤痛
    2021-01-24 01:50

    The problem is a combination of two factors:

    The first is that for vararg functions like printf, the compiler will not do any implicit conversions of the arguments. So the 0 in the argument list is an integer constant (of type int).

    The second factor is the mismatching format specifier. The printf function doesn't know anything about the arguments being passed, except what is specified in the format string. Mismatching format and argument type leads to undefined behavior. And since the "%f" specifier make printf expect a value of type double, and you have given an int value, you have such a mismatch.

提交回复
热议问题