#include <stdio.h>
int main(void) {
float base, height, hyp;
printf("input base of triangle:\n");
scanf("%f", base);
printf("input height of triangle:\n");
scanf("%f", height);
printf("input hypotenuse of triangle:\n");
scanf("%f", hyp);
float perimeter = base + height + hyp;
printf("the perimeter of your triangle is: %f\n", perimeter);
return 0;
}
我通过ideone.com运行它并显示成功,然后标准输入为空,然后在stdout中打印所有我没有数字的打印语句
答案 0 :(得分:3)
这是因为ideone 不是交互式的。与从命令行运行程序不同,ideone要求您在&#34;输入&#34;中预先提供所有输入。标签:
您需要在运行程序之前输入所有数据。
P.S。完成后,请注意您是如何处理未定义的行为,因为您将值(而不是指针)传递给scanf
。在ideone上解决这个问题的最佳方法是选择&#34; C99 strict&#34;编译C代码时的选项。这将通过以下警告中断编译:
prog.c:7:11:错误:格式&#39;%f&#39;期望类型&#39; float *&#39;的参数,但参数2的类型为&#39; double&#39; [-Werror =格式=]
scanf("%f", base);
答案 1 :(得分:1)
scanf
需要指向您数据类型的指针,您应该使用&
传递变量的地址:
scanf("%f", &base);
scanf("%f", &height);
scanf("%f", &hyp);
要添加,一些错误检查可能很有用,例如:
if(scanf("%f", &base) != 1) //number of items scanned is expected to be 1
//process error..
//etc