A Kla 2021-10-21 12:19 采纳率: 100%
浏览 30
已结题

win10系统,使用命令行gcc 和使用cfree 编译生成的.exe运行结果不同


#include <stdio.h>
#include <stdlib.h>

#define SIZE 8

void itob(int inp,char *numbin);
int main(void)
{
    int inp1,site,outcome;
    char numbin[SIZE];

    puts("Please enter an integer.");
    fscanf(stdin,"%d",&inp1);      //传入待处理的数
   
    puts("Please enter the number of site.");
    fscanf(stdin,"%d",&site);      //传入待处理数的二进制第几位

    itob(inp1,numbin);     //将待处理数转为二进制,保存至数组
    printf("%d\n",site);     //显示待处理数的二进制第几位
    printf("The binary number is %s\n",numbin);
    fprintf(stdout,"The %d site is %c\n",site,numbin[site - 1]);
    system("pause");

    return 0;
}

void itob(int inp,char *numbin)  //十进制转二进制,字符数组保存
{
    int i = SIZE - 2;

    while(inp)
    {
        numbin[i] = (1 & inp) + '0';
        i--;
        inp >>= 1;
    }
    numbin[SIZE] = '\0';

    while(i >= 0)
        numbin[i--] = '0';
}

键盘输入数字32 ,位数2
使用命令行gcc编译后的a.exe,结果:
Please enter an integer.
32
Please enter the number of site.
2
0
The binary number is 0100000
The 0 site is
请按任意键继续
使用cfree编译后的结果:
Please enter an integer.
32
Please enter the number of site.
2
2
The binary number is 01000000
The 2 site is 1

gcc编译的里面,不知道为什么itob函数会改变传入的site的值,但是并没有把site的地址传进去,求各位解答

  • 写回答

1条回答 默认 最新

  • 赵4老师 2021-10-21 13:40
    关注

    在每个最后不带\n的printf后面加fflush(stdout);
    在每个不想受接收缓冲区旧内容影响的scanf前面加rewind(stdin);
    另外请检查scanf的返回值。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 5月24日
  • 已采纳回答 5月16日
  • 创建了问题 10月21日

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄