gpt4 book ai didi

c - 为什么总是结冰

转载 作者:行者123 更新时间:2023-11-30 20:59:30 24 4
gpt4 key购买 nike

我尝试将定义切换为 const,但似乎也不起作用

#include <stdio.h>
#include <stdlib.h>

#define TIMES 5/9
#define MINUS 32
int main()
{
int temp_fahr = 0;
printf("Enter the temperature in fahrenheit\n");
scanf("%d\n",temp_fahr);
printf("The temperature in celcious is: %.3f\n",(temp_fahr-MINUS)*TIMES);
return 0;
}

最佳答案

你的问题是线路

scanf("%d\n",temp_fahr);

编译器警告会告诉您,它需要 int * 类型的输入,但收到的是整数。因此,它尝试写入 temp_fahr 指向的地址,这会导致段错误。

其次,如果您通过提供指向 temp_fahr 的指针来修复它,scanf 会一直等待从标准输入中修剪掉的换行符,因此它会卡在那里。所以,正确的行是

scanf("%d",&temp_fahr);

您还会注意到 %f 格式需要一个 double 类型的参数并接收一个整数,因此您需要将结果转换为 double在将其传递给 printf 之前的某个时刻。

关于c - 为什么总是结冰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47316006/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com