You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言编写矩形面积计算代码时scanf报错无法运行怎么解决?

修复后可正常运行的代码

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(){
    /*Declaration of Input and Output variables*/
    float width, height, rectangle; /*width and height are the input numbers, rectangle is the output*/
    /*Prepare option of input*/
    printf("Please type in the width: "); /*width input*/
    scanf("%f", &width);
    printf("Please type in the height: "); /*height input*/
    scanf("%f", &height);
    /*prepare the output*/
    rectangle = width * height; 
    /*display the results*/
    printf("Going by the formula w * h, %f times %f gives: %f\n", width, height, rectangle);
    return 0;
}

具体修复点

  • 删掉了重复且格式错误的#include stdio.h行:你第一行已经正确引入了stdio.h,第二行的引入既没有加尖括号,又属于重复引入,会直接导致编译报错。
  • 修复了printf参数不匹配的问题:你原代码的printf里写了3个%f占位符,但只传入了rectangle一个变量,需要补充width、height两个变量作为参数,和占位符一一对应;同时删除了最后一个% f中间多余的空格,避免输出时数字前额外多出空格。
  • 在所有头文件最顶部添加了#define _CRT_SECURE_NO_WARNINGS宏定义:这个宏是Visual Studio专用的,添加后会禁用「scanf不安全」的报错,不会影响你用scanf实现输入逻辑,完全符合项目要求。

内容的提问来源于stack exchange,提问作者Metiri Magoro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 19:54:03