gpt4 book ai didi

c - 我已经编写了一个代码来将摄氏度转换为华氏度,但是输出太大了

转载 作者:太空宇宙 更新时间:2023-11-04 01:20:58 25 4
gpt4 key购买 nike

代码是:

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

int main() {
double C, F;
printf("Enter the temperature in Celisius: ");
scanf("%f", &C);
F = 32 + (C * (180.0 / 100.0));
printf("%f C = %f F\n", C, F);
system("pause");
return 0;
}

输出是:

Enter the temperature in Celisius: 100
-92559604910177974000000000000000000000000000000000000000000000.000000 C = -166607288838320360000000000000000000000000000000000000000000000.000000 F

最佳答案

scanf()double 转换说明符错误。你需要使用

  scanf("%lf", &C);

要添加相关引用,来自 C11,第 §7.21.6.2 章

l (ell)

Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long int or unsigned long
int
; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double; or that a following c, s, or [ conversion specifier applies to an argument with type pointer to wchar_t.

也就是说,您必须检查scanf() 的返回值以确保调用成功。否则,您最终将使用未初始化变量的值。

关于c - 我已经编写了一个代码来将摄氏度转换为华氏度,但是输出太大了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42641534/

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