a昊a 2019-07-06 10:37 采纳率: 66.7%
浏览 259
已采纳

哪位大神可以说明一下这个字符输出问题?

先上代码

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int main(void)
{
    bool flag = false;
    int unprt[32];
    int ch;
    long count = 0;
    for(int i = 0;i <= 31; i++)
    unprt[i] = i;
    while((ch = getchar()) != EOF)
    {
        for(int i = 0;i <= 31; i++)
        {
        if(ch==unprt[i])
        {
        printf("^%s ",ch+64);
        printf(" %d ",ch);
        count++;
        flag = true;
        break;
        }
            }
        if(flag)
        continue;
        else {putchar( ch );
        printf(" %d ",ch);
        count++;
        }
        flag = false;   
        if(count>10&&(count%10==0))
        putchar('\n');
    }
    system("pause");
    return 0;
}

程序的目的是接受字符并打印字符及其对应的ASCII码,如果是非打印字符就用一个向上的箭头加
字母表示下面是运行结果
图片说明
这个前面几个字符还好但是最后的ctrl+A就程序停止运行了,这是什么情况?

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-07-06 11:46
    关注

    printf("^%c ",ch+64); // %s是输出字符串。你没有\0就崩溃了

    图片说明

    // Q768341.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
        bool flag = false;
        int unprt[32];
        int ch;
        long count = 0;
        for(int i = 0;i <= 31; i++)
        unprt[i] = i;
        while((ch = getchar()) != EOF)
        {
            for(int i = 0;i <= 31; i++)
            {
            if(ch==unprt[i])
            {
            printf("^%c ",ch+64);
            printf(" %d ",ch);
            count++;
            flag = true;
            break;
            }
                }
            if(flag)
            continue;
            else {putchar( ch );
            printf(" %d ",ch);
            count++;
            }
            flag = false;   
            if(count>10&&(count%10==0))
            putchar('\n');
        }
        system("pause");
        return 0;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog