C语言 如何比较 ASCII 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7465494/
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
How to compare an ASCII value
提问by XIIIX
I want to store the ASCII value of a letter into a variable, how can I do this?
我想将一个字母的 ASCII 值存储到一个变量中,我该怎么做?
for example :
例如 :
r ASCII variable = 82
main()
{
character = "character read from a file";
variable= "r ascii"; //(in this case 82), the problem is that the letter is always variable.;
printf( "the value of %c is %d, character, variable)
}
How can I do this?
我怎样才能做到这一点?
Also on an extra note, how could I read a .txtfile character by character? so It could be saved on the character variable.
另外要注意的是,我如何.txt逐个字符地读取文件?所以它可以保存在字符变量上。
回答by Pablo Santa Cruz
Just do:
做就是了:
if (r == 82) {
// provided r is a char or int variable
}
In C, charvariables are represented by their ASCIIinteger value, so, if you have this:
在C 中,char变量由它们的ASCII整数值表示,所以,如果你有这个:
char r;
r = 82;
if (r == 82) {
}
Is the same as:
是相同的:
char r;
r = 'R';
if (r == 'R') { // 'R' value is 82
}
You can even mix them:
你甚至可以混合它们:
char r;
r = 82;
if (r == 'R') { // will be true
}
回答by bijin
If you just want to save the asciivalue onto an integer variable
如果您只想将ascii值保存到整数变量中
just use this
就用这个
int b;
char c = 'r';
b = (int)c;
printf("%d",b);

