我已经编写了一个代码来将摄氏度转换为华氏度,但是输出太大了
I have written a code to convert from Celisius to Fahrenheit but the output is too huge
密码是:
#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()
的return值,以确保调用成功。否则,您将最终使用未初始化变量的值。
密码是:
#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
, orn
conversion specifier applies to an argument with type pointer tolong int
orunsigned long int
; that a followinga
,A
,e
,E
,f
,F
,g
, orG
conversion specifier applies to an argument with type pointer todouble
; or that a followingc
,s
, or[
conversion specifier applies to an argument with type pointer towchar_t
.
也就是说,您必须检查scanf()
的return值,以确保调用成功。否则,您将最终使用未初始化变量的值。