c - 程序打印 0 作为通过 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;
}
推荐阅读
- sql - MariaDB 触发器中的 JSON_EXTRACT() 表示 json 中的错误,JSON_VALID() 表示其有效
- reactjs - 如何填充嵌套对象
- ios - 如何从 PWA(渐进式 Web 应用程序)构建 ipa(iOS App Store 包)
- c++ - 程序打印出间隔 (5500) 可被 2 和 3 整除的数字 (c++)
- regex - Scala-Spark 数据帧中的 unicode (emoji) 的 Regexp_replace PART
- mysql - 使用嵌套对象更新记录,给出重复键错误
- bash - Jenkins管道多行脚本命令作为变量
- python-3.x - conda环境下安装pdfminer库出错
- javascript - 如何使用内联 onclick 将 pug mixin 的 id 转换为 javascript 函数
- python - Flask:当我尝试访问索引时出现错误 404