我是 C 新手。我正在尝试编写一个程序来计算斜边,但是当我运行它时,我得到了垃圾值,并且我不知道如何处理它。
double Hypotenuse(double base, double perpendicular)
{
double hyp = sqrt((base*base) + (perpendicular*perpendicular));
return hyp;
}
int _tmain(void)
{
double b, p;
printf("Enter the lenght of base\n");
scanf_s("%f",&b);
printf("Enter the lenght of perpendicular\n");
scanf_s("%f",&p);
printf("The hypotenuse of the triangle is %3.3f",Hypotenuse(b,p));
return 0;
}
最佳答案
问题出在你的 scanf 语句中。 作为一条经验法则,您应该始终对 double 使用“%lf”。 这非常有效:
double Hypotenuse(double base, double perpendicular)
{
double hyp = sqrt((base*base) + (perpendicular*perpendicular));
return hyp;
}
int _tmain(void)
{
double b, p;
printf("Enter the lenght of base\n");
scanf_s("%lf",&b);
printf("Enter the lenght of perpendicular\n");
scanf_s("%lf",&p);
printf("The hypotenuse of the triangle is %3.3lf",Hypotenuse(b,p));
return 0;
}
关于c - 我该如何处理垃圾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135210/