using printf to print out floating values

后端 未结 3 749
梦谈多话
梦谈多话 2020-11-30 10:51
#include
#include

int main(void)
{
  int x, *ptr_x;
  float f , *ptr_f;

  ptr_f = &f;
  ptr_x = &x;
  *ptr_x = 5;
  *ptr_f =         


        
3条回答
  •  执笔经年
    2020-11-30 11:28

    Once you pass at least one invalid format specifier to printf (like attempt to print a float value with %d or an int value with %f) your entire program gets screwed up beyond repair. The consequences of such destructive action can be seen anywhere in the program. In your case an attempt to print something with an invalid format specifier resulted in that even the valid format specifiers stopped working.

    Speaking formally, you wrote a program that exhibits undefined behavior. It can act absolutely unpredictably. You said it yourself

    The point of the this code is to show what would happen if a floating value is printed with %d and if a int value is printed %f.

    The broken behavior that you observe demonstrates exactly that! A bizarrely and unpredictably acting program is exactly what happens when you attempt to do something like that.

提交回复
热议问题