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

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

    To understand the floating point issue, consider reading: http://en.wikipedia.org/wiki/IEEE_floating_point

    As for hexadecimal, let me guess.. the output was something like... 99?

    This is because of encodings.. the machine has to represent information in some format, and usually that format entails either giving meanings to certain bits in a number, or having a table of symbols to numbers, or both

    Floating points are sometimes represented as a (sign,mantissa,exponent) triplet all packed in a 32 or 64 bit number - characters are sometimes represented in a format named ASCII, which establishes which number corresponds to each character you type

提交回复
热议问题