c - 不同的语句导致类型转换不同。c/gcc
问题描述
如果我在语句中添加“double”,那么它将打印 60.00,否则代码将打印 0.0000..
#include <stdio.h>
int main(void)
{
void foo();// if i add "double" into statement then will printf 60.00
char c = 60;
foo(c);
return 0;
}
void foo(double d)
{
printf("%f\n", d);//now printf 0.0000
}
解决方案
你的代码调用未定义行为,因为调用者不知道 foo 的参数。您的原型说该函数将采用未指定数量的参数。它没有说明这些参数的类型。所以它进行隐式转换int
并传递int
给 foo。
foo
需要双参数。
该怎么办?
在函数调用之前添加函数原型。
void foo(double);
int main(void)
{
char c = 60;
foo(c);
return 0;
}
void foo(double d)
{
printf("%f\n", d);//now printf 60.0000
}
推荐阅读
- reactjs - Ref 在 React 中奇怪地变成了 null
- java - 如何将数学符号(例如 R^n)放入 Java 字符串?
- r - 如何在ggplot的散点图中根据因子列最好地绘制值列的两个子集?
- javascript - 将所选内容以 PDF 格式保存到 Google 表格电子表格
- php - 如何从 php 中的选择/选项中获取多个数据到一个变量中?
- python - 从 python 下载 xls 导致格式损坏与手动下载
- python - 当我尝试创建虚拟环境时返回非零退出状态 101 给出错误
- karate - 我如何在空手道 UI 自动化中拦截 API 调用
- python - 在线程内使用 Popen 的奇怪行为
- r - 在数据帧上应用函数,包括 rbind 语句