gpt4 book ai didi

c - ASCII 值未给出正确的字母

转载 作者:行者123 更新时间:2023-11-30 16:45:04 29 4
gpt4 key购买 nike

我是初学者。

我用c语言用eclipse写了一个程序。它在编译时和执行后都不会给我任何错误和警告。
它打印出所有字符的所有 ASCII 值。

代码

#include <stdlib.h>
#include <stdio.h>
int main(){

int a=0;
system("clear");
printf("\nthese are the ASCII values of characters given in front of them\n");
while(a<=255)
{
printf("%c %d \n",a,a);

a=a+1;

}
}

输出很奇怪,当我复制粘贴输出时它消失了,因此这是 link输出的屏幕截图。我无法截取整个屏幕,但在 126 之后,字符看起来像盒子。我的代码有问题吗?

最佳答案

许多字符是不可打印的,有些是终端/控制台的控制代码,用于执行诸如清屏、移动光标、清线...等操作。

使用ctype.h中定义的isprint函数来确定该字符是否可打印。 for 循环也更适合您的要求,请参见下面的示例。

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(){

system("clear");
printf("\nthese are the ASCII values of characters given in front of them\n");
int a;
for(a = 0; a <= 255; ++a)
{
printf("%c %d \n", isprint(a) ? a : '.', a);
}
}

关于c - ASCII 值未给出正确的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44240494/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com