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

后端 未结 5 1772
天命终不由人
天命终不由人 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:18

    Because printf, like any function that work with varargs, eg: int foobar(const char fmt, ...) {} tries to interpret its parameter to certain type.

    If you say "%f", then pass c (as a char), then printf will try to read a float.

    You can read more here: var_arg (even if this is C++, it still applies).

提交回复
热议问题