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

C语言实现5位数字反转代码无语法错误却无法运行,求排查问题

错误原因说明
  • 核心错误为scanf函数传参错误:C语言的scanf读取整型变量时,需要传入变量的地址作为参数,你的代码中scanf("%d",m);缺少取地址符&,导致程序无法将用户输入的数值正确写入变量m,反而会尝试向m的初始随机值对应的内存地址写入数据,大概率触发内存访问错误直接崩溃。
  • 你的数字拆分、反转拼接逻辑本身没有问题,只需要修正scanf这一行即可正常运行。
修改后的完整代码
//Reverse the number
#include<stdio.h>
int main()
{
    int m,a,b,c,d,e,n;

    printf("\nEnter a five digit number: ");
    scanf("%d",&m); // 仅修改了这一行,添加&取地址符

    a = m/10000;                                        //1st digit     
    b = (m - a*10000)/1000;                             //2nd digit
    c = ((m-a*10000) - b*1000)/100;                     //3rd digit
    d = (((m-a*10000) - b*1000) - c*100)/10;            //4th digit
    e = ((((m-a*10000) - b*1000) - c*100) - d*10);      //5th digit

    n = e*10000 + d*1000 + c*100 + b*10 + a;            //reverse of the number
    
    printf("Reverse of the number given by you is %d\n", n);

    return 0;
}
补充说明

如果需要兼容非5位数字的反转场景,可改用循环取余、逐位拼接的通用实现,不需要提前固定位数拆分。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 09:12:01