程序打印 0 作为通过 scanf("%f") 获得的两个正整数相乘的结果
Program prints 0 as result of multiplication of two positive integers obtained via scanf("%f")
我的代码:
#include <stdio.h>
int main()
{
int l, b, a;
printf("Enter the length of the rectangle: ");
scanf("%f", &l);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &b);
printf("Area of rectangle is %f", l * b);
return 0;
}
当我提供任何输入时,它不会向我显示其产品,而是 0.000000
:
当我输入 2 和 3 时,它应该打印 Area of rectangle is 6
%f
期望其相应的参数具有类型 float
并且您将 int
传递给它,因此将其更改为 %d
将解决问题,因为 %d
期望其对应的参数具有类型 int.
#include <stdio.h>
int main() {
int length, breadth, area;
printf("\nEnter the Length of Rectangle: ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle: ");
scanf("%d", &breadth);
area = length * breadth;
printf("\nArea of Rectangle: %d\n", area);
return 0;
}
我的代码:
#include <stdio.h>
int main()
{
int l, b, a;
printf("Enter the length of the rectangle: ");
scanf("%f", &l);
printf("Enter the breadth of the rectangle: ");
scanf("%f", &b);
printf("Area of rectangle is %f", l * b);
return 0;
}
当我提供任何输入时,它不会向我显示其产品,而是 0.000000
:
当我输入 2 和 3 时,它应该打印 Area of rectangle is 6
%f
期望其相应的参数具有类型 float
并且您将 int
传递给它,因此将其更改为 %d
将解决问题,因为 %d
期望其对应的参数具有类型 int.
#include <stdio.h>
int main() {
int length, breadth, area;
printf("\nEnter the Length of Rectangle: ");
scanf("%d", &length);
printf("\nEnter the Breadth of Rectangle: ");
scanf("%d", &breadth);
area = length * breadth;
printf("\nArea of Rectangle: %d\n", area);
return 0;
}