在 C/C++ 中打印所有 ASCII 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27054414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing all the ASCII Values in C/C++
提问by Rohit Saluja
Hello Everyone its been Long that I am not in touch with the C/C++ Language and was just revising the concepts again and I came across this question which asked to write a program to display all the ASCII characters and I wrote the following good, but it is not giving expected result. Can anyone please tell what is the Problem with this code.
大家好,我很长时间没有接触 C/C++ 语言,只是再次修改概念,我遇到了这个问题,该问题要求编写一个程序来显示所有 ASCII 字符,我写的很好,但是它没有给出预期的结果。谁能告诉我这段代码有什么问题。
#include<iostrem.h>
int main()
{
unsigned char a;
for(a = 0; a < 256; ++a)
{
cout << a << " ";
}
return 0;
}
回答by Adam D. Ruppe
a
is alwaysless than 256, since an unsigned char cannot possibly go higher than 255. You've written an infinite loop.
a
是永远小于256,因为unsigned char类型不可能走高于255你已经写了一个无限循环。
Your include also has a mispelling and extra .h
and you didn't use the std
namespace on cout
.
您的包含也有拼写错误和额外内容,.h
并且您没有std
在cout
.
edit: Finally, technically, ASCII only counts the first 128 characters, everything beyond that is the domain of various extended character sets.
编辑:最后,从技术上讲,ASCII 只计算前 128 个字符,除此之外的一切都是各种扩展字符集的域。
回答by Ryan
If you can use <stdio.h>
, then it is easier.
如果你可以使用<stdio.h>
,那就更容易了。
#include <stdio.h>
int main()
{
for(int i = 0; i <= 255; i++) {
fprintf(stdout, "[%d]: %c\n", i, i);
}
return 0;
}
回答by Galik
The other answers have this well covered. I just thought I would throw in that it might be good to check if the characters are printable before printing them:
其他答案已经很好地涵盖了这一点。我只是想我会认为在打印字符之前检查字符是否可打印可能会很好:
#include <cctype>
#include <iostream>
int main()
{
for(int a = 0; a < 256; ++a) // use int (big enough for 256)
if(std::isprint(a)) // check if printable
std::cout << char(a) << " "; // print it as a char
}
回答by Himanshu
Try this code:
试试这个代码:
=>C++
=> C++
#include<iostream>
int main ()
{
int a;
for(a=0;a<256;++a)
{
cout<<(char)a<<" ";
}
return 0;
}
=>C
=> C
#include<stdio.h>
int main ()
{
int a;
for(a=0;a<256;++a)
{
printf("%c " a);
}
return 0;
}
回答by phuclv
There are a lot of problems with your code.
你的代码有很多问题。
First there is no iostrem.h
. After correcting this to iostream.h
g++ will give a fatal error: iostream.h: No such file or directory
because the standard libraries must not be included with .h
首先没有iostrem.h
。将此更正为iostream.h
g++ 后将给出一个fatal error: iostream.h: No such file or directory
因为标准库不得包含在.h
Changing it to #include <iostream>
results in the error: 'cout' was not declared in this scope
. You need std::cout << a
to make it compile successfully.
将其更改#include <iostream>
为error: 'cout' was not declared in this scope
. 你需要std::cout << a
让它编译成功。
But even after resolving all the above problems, an important thing was output by gcc with -Wall -Wextra -pedantic
option
但是即使解决了上述所有问题,还有一个重要的事情是 gcc 输出带有-Wall -Wextra -pedantic
选项
warning: comparison is always true due to limited range of data type
That's because 256 is outside unsigned char
's typical range. It only works if char has more than 8 bits, which is not the case of your platform. You should always enable all compiler warnings. That'll help you identify a lot of problems without the need to ask here.
那是因为 256 超出了unsigned char
的典型范围。它仅在 char 超过 8 位时才有效,这不是您的平台的情况。您应该始终启用所有编译器警告。这将帮助您确定很多问题,而无需在这里提问。
Nevertheless, don't use types less than int
for temporary values unless very necessary because they all will be promoted to int
in expressions anyway.
尽管如此,int
除非非常必要,否则不要使用小于临时值的类型,因为int
无论如何它们都会在表达式中被提升。
回答by Margaret
it works.
有用。
#include <iostream>
using namespace std;
int main() {
char a;
int i;
for (int i=0; i<256; i++){
a=i;
cout << i << " " << a << " " <<endl;
}
return 0;
}
回答by MRL
This is obviously a very old question, but if anyone wanted to actually print the ASCII code along with its corresponding numbers:
这显然是一个非常古老的问题,但是如果有人想实际打印 ASCII 代码及其相应的数字:
#include <iostream>
int main()
{
char a;
for (a=0; a<=127; a++)
{
std::cout<<a<<" ";
std::cout<<int(a)<<" "<<std::endl;
}
return 0;
}
NB/to the reader:
注意/致读者:
code 0-31: unprintable chars; used for formatting & control printers
代码 0-31:不可打印的字符;用于格式化和控制打印机
code 32-127: printable chars; represents letters, numbers & punctuation.
代码 32-127:可打印字符;代表字母、数字和标点符号。
回答by Vikas Reddy
#include <stdio.h>
int main ()
{
unsigned char a;
for(a=0;a<=255;a++)
{
printf(" %c %d \n ",a,a);
}
getch();
return 0;
}
回答by Rain
What result are you getting? I think you need to output
你得到什么结果?我认为你需要输出
Cout<<(chat)a;
Otherwise it would only return the integer number that it assigned
否则它只会返回它分配的整数