我的C程序只输出0.000,我不知道为什么
#include <stdio.h>
#include <float.h>
int main(){
int count;
double input;
double output = DBL_MAX;
int i;
printf("Enter a positive integer: ");
scanf("%i", &count);
for (i = 0; i < count; i++){
printf("Enter a number:");
scanf("%f", &input);
if(output>input)
{
(output = input);
}
}
printf("The smallest number is: %f", output);
}
代码编译得很好,但似乎没有不正确扫描或其他一些我没有看到的问题。
答案 0 :(得分:2)
类型double
的正确说明符是%lf
。
您将double变量的地址传递给scanf()并告诉它它是float %f
的地址。
然后函数溢出,结果不正确。
答案 1 :(得分:1)
只需将%f
更改为%lf
(printf
和scanf
),因为输出和输入都被声明为double
。
#include <stdio.h>
#include <float.h>
int main(){
int count;
double input;
double output = DBL_MAX;
int i;
printf("Enter a positive integer: ");
scanf("%i", &count);
for (i = 0; i < count; i++){
printf("Enter a number:");
scanf("%lf", &input);
if(output>input)
{
(output = input);
}
}
printf("The smallest number is: %lf", output);
}
Idoene链接:http://ideone.com/BspQwf