I get previous float value when I am printing new value

后端 未结 2 1588
孤独总比滥情好
孤独总比滥情好 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 02:00

    in

    > printf("%f",0);
    

    You ask to print a double but you give an int, this is contradictory

    You are not in the case where the generated code makes a double from the int because printf is not int printf(const char *, double); but int printf ( const char * format, ... ); and the compiler does not look at the format to make the necessary conversions ( but in a lot of cases the compiler warn you )

    When prints access to the second argument is does to get a double using 64b and probably your int use only 32b, the behavior is undefined.


    (edit, thank you @chqrlie)

    I get previous float value when i am printing new value

    In your case may be printf retrieves a double value from the MMX registers as opposed to the int value that was passed via the stack or regular registers... which may explain why the same value gets printed twice. But of course as always with undefined behavior, anything else could happen at any time

提交回复
热议问题