c - 隐式和显式转换

标签 c casting

使用:

float return1(void);

int  main()
{ 
      int    x;
      x   =   (float)return1();
      printf("%f",x);
      return 0;
}

float return1()
{
       return 1;
}`

为什么输出是-0.000000

不应该将x隐式转换为float并打印1.000000吗?

最佳答案

Shouldn't x be implicitly cast to a float and print 1.000000?

不,不应该,因为编译器可能不知道 printf 的作用或将使用哪种格式来打印 x

x 的类型为 int,因此应使用 %d 而不是 %f 来打印它。

关于c - 隐式和显式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33217916/

相关文章:

python - 使用pyserial向Arduino发送信息(操作步进电机)

java - 为什么需要多重类型转换?

c - bison/flex 中的多种数据类型

c++ - 将字符串文字分配给 char* : const_cast<char *> vs using char array

ios - 从 Parse 中获取子类对象

c - 如何获得用宏存储整数值所需的最少字节?

c - 如何在 sscanf 中传递可变长度宽度说明符?

c++ - 从(开放的)Solaris 交叉编译到 Windows?

c - 是否可以将内存位置与缓存 Hook

c++ - 强制转换为 C++ 中的字符串运算符重载