查找最大数字(C) - 代码无效

0 c

我需要用户输入三个数字和我的程序来显示这些数字中最大的数字.我似乎无法弄清楚问题.我得到的结果是"最大的数字是0.000"

#include <stdio.h>

int main()
{

double n1, n2, n3;

printf("Enter your three numbers: ");
scanf("%1f %1f, %1f", &n1, &n2, &n3);

if (n1>= n2 && n1>= n3)
    printf("The greatest number is %f", n1);

if (n2>=n1 && n2>= n3)
    printf("The greatest number is %f", n2);

if (n3>=n2 && n3>=n1)
    printf("The greatest number is %f", n3);


return 0;
}
Run Code Online (Sandbox Code Playgroud)

Vor*_*ung 5

编译器知道!

$ gcc -Wall temp.c
temp.c:9:23: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%1f %1f, %1f", &n1, &n2, &n3);
       ~~~            ^~~
       %1lf
temp.c:9:28: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%1f %1f, %1f", &n1, &n2, &n3);
           ~~~             ^~~
           %1lf
temp.c:9:33: warning: format specifies type 'float *' but the argument has type 'double *' [-Wformat]
scanf("%1f %1f, %1f", &n1, &n2, &n3);
                ~~~             ^~~
                %1lf
3 warnings generated.
Run Code Online (Sandbox Code Playgroud)

  • 编译器不知道的是,指定最大字段宽度为1可能不是预期的.也许添加关于`%lf` vs`%1lf`的说明. (2认同)