c中常量的默认数据类型是什么?

San*_*eep 3 c types

float x=2, y=2.6;

if(x==2); 
if(x==2.0) //both these conditions evaluate to be true
Run Code Online (Sandbox Code Playgroud)

但,

 if(y==2.6); 
Run Code Online (Sandbox Code Playgroud)

评估为假

谁能解释什么是常数的默认数据类型,为什么我会得到这样的结果

pmg*_*pmg 6

float x=2, y=2.6;
//           ^^^ double, internally converted to float during the initialization
//      ^ int, internally converted to float during the initialization

if (x == 2) // compare double converted from float (x) with double converted from int (2)
if (x == 2.0) // compare double converted from float (x) with double (2.0)

if (y == 2.6) // compare double converted from float (y) with double (2.6)
Run Code Online (Sandbox Code Playgroud)

注意最后一个条件:最初2.6(双精度)被转换为浮点数;将该浮点数转换回双精度值并不能保证相同的值。

(double)(float)2.6 != 2.6
Run Code Online (Sandbox Code Playgroud)

它有点像这样:

                 2.6 is 0b10.1001100110011001100110011001100110011001100110011001
          (float)2.6 is 0b10.10011001100110011001100
  (double)(float)2.6 is 0b10.1001100110011001100110000000000000000000000000000000
          difference is                             ^ approx. ~0.000000095367
Run Code Online (Sandbox Code Playgroud)