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

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

    You need to use a cast operator like this:

    char c = 'z';
    
    printf("%f %X", (float)c, c);
    

    or

    printf("%f %X", (double)c, c);
    

    In Xcode, if I do not do this, I get the warning:

    Format specifies specifies 'double' but the argument has type 'char', and the output is 0.000000.

提交回复
热议问题