You need to enable JavaScript to run this app. 计算圆面积体积的C代码报expected declaration or statement at end of input错误
优惠活动
大模型
产品
解决方案
定价
更多

计算圆面积体积的C代码报expected declaration or statement at end of input错误

报错原因分析
  • 核心报错expected declaration or statement at end of input由代码块括号不匹配触发:你提供的代码片段中main函数的大括号看起来是完整的,但你本地的practise2.c文件大概率存在main函数末尾闭合大括号}遗漏、或者存在未闭合的块注释/其他括号的问题,编译器扫描到文件结束都没找到和第4行{匹配的闭合标记,因此抛出该错误。
  • 除此之外你的代码还存在几处会导致运行结果异常的问题:
    1. 格式符不匹配:半径、圆周率的乘积为浮点类型,你使用了整型格式符%d输出,会导致结果显示乱码。
    2. 逻辑错误:不存在“圆的体积”概念,你要计算的应为圆柱体积,且圆柱体积公式为π*r²*h,你多乘了一次半径导致公式错误。
    3. 体验问题:输入提示末尾没有换行,用户输入内容会和提示文字挤在同一行。
修复方案

首先检查本地practise2.c的括号匹配情况,确保所有大括号、小括号、块注释都正常闭合,再替换为以下修正后的代码:

#include <stdio.h>

int main()
{
    int r;
    float pi = 3.142;
    printf("Enter the radius of circle to find its area:\n");
    scanf("%d",&r);
    printf("The area of the circle is %f \n", r*r*pi );
    int h;
    printf("Enter the height of the cylinder to find its Volume:\n");
    scanf("%d",&h);
    printf("The volume of the cylinder is %f \n", pi*r*r*h);
    return 0;
}

修正后使用原命令重新编译即可正常运行:
gcc practise2.c -o practise2 && "/home/spyder/Desktop/"practise2

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 21:06:00