仅 Printf returns 个零
Printf only returns zeros
我正坐在我的电脑前,但我不知道我的错误在哪里。所以这是我在 C:
中的代码
#include <stdio.h>
#include <math.h>
double f (double x);
int main ()
{
double x;
printf("Please type in a decimal number: ");
scanf("&lf", &x);
printf("%", f(x));
return 0;
}
double f (double x)
{
return (fabs(x) * sin(x) * 10e-2);
}
代码我看了好几遍,要么是我太笨了,要么就是找不到错误。请帮我。如果你能帮助我,我会很高兴。
希望你们能给我一些想法或提示(即使那样也会有所帮助)。
改变这个:
scanf("&lf", &x);
对此:
scanf("%lf", &x);
还有这个:
printf("%", f(x));
对此:
printf("%lf", f(x));
与其坐在电脑前,不如寻求编译器的帮助,通过使用 -Wall
标志进行编译以启用大量警告。然后,您发布的代码应该给您:
prog.c: In function 'main':
prog.c:11:11: warning: too many arguments for format [-Wformat-extra-args]
11 | scanf("&lf", &x);
| ^~~~~
prog.c:13:13: warning: spurious trailing '%' in format [-Wformat=]
13 | printf("%", f(x));
| ^
prog.c:13:12: warning: too many arguments for format [-Wformat-extra-args]
13 | printf("%", f(x));
| ^~~
这会帮助您查明问题并开始搜索互联网,直到例如 Reading in double values with scanf in c 出现!
PS: 我假设你使用 GCC 编译器。如果没有,请确保启用编译器的编译警告。
我正坐在我的电脑前,但我不知道我的错误在哪里。所以这是我在 C:
中的代码#include <stdio.h>
#include <math.h>
double f (double x);
int main ()
{
double x;
printf("Please type in a decimal number: ");
scanf("&lf", &x);
printf("%", f(x));
return 0;
}
double f (double x)
{
return (fabs(x) * sin(x) * 10e-2);
}
代码我看了好几遍,要么是我太笨了,要么就是找不到错误。请帮我。如果你能帮助我,我会很高兴。
希望你们能给我一些想法或提示(即使那样也会有所帮助)。
改变这个:
scanf("&lf", &x);
对此:
scanf("%lf", &x);
还有这个:
printf("%", f(x));
对此:
printf("%lf", f(x));
与其坐在电脑前,不如寻求编译器的帮助,通过使用 -Wall
标志进行编译以启用大量警告。然后,您发布的代码应该给您:
prog.c: In function 'main':
prog.c:11:11: warning: too many arguments for format [-Wformat-extra-args]
11 | scanf("&lf", &x);
| ^~~~~
prog.c:13:13: warning: spurious trailing '%' in format [-Wformat=]
13 | printf("%", f(x));
| ^
prog.c:13:12: warning: too many arguments for format [-Wformat-extra-args]
13 | printf("%", f(x));
| ^~~
这会帮助您查明问题并开始搜索互联网,直到例如 Reading in double values with scanf in c 出现!
PS: 我假设你使用 GCC 编译器。如果没有,请确保启用编译器的编译警告。