why printf behaves differently when we try to print character as a float and as a hexadecimal?

后端 未结 5 1852
天命终不由人
天命终不由人 2021-01-29 12:41

I tried to print character as a float in printf and got output 0. What is the reason for this.
Also:

char c=\'z\';
printf(\"%f %X\",c,c);
5条回答
  •  太阳男子
    2021-01-29 13:09

    I tried to print character as a float in printf and got output 0. What is the reason for this.

    The question is, what value did you expect to see? Why would you expect something other than 0?

    The short answer to your question is that the behavior of printf is undefined if the type of the argument doesn't match the conversion specifier. The %f conversion specifier expects its corresponding argument to have type double; if it isn't, all bets are off, and the exact output will vary.

提交回复
热议问题