如何比较ASCII值

我想将字母的ASCII值存储到变量中,我该怎么做?

例如 :

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) } 

我怎样才能做到这一点?

另外还要注意,如何逐个字符地读取.txt文件? 所以它可以保存在字符变量上。

做就是了:

 if (r == 82) { // provided r is a char or int variable } 

C中char变量由它们的ASCII整数值表示,所以,如果你有这个:

 char r; r = 82; if (r == 82) { } 

是相同的:

 char r; r = 'R'; if (r == 'R') { // 'R' value is 82 } 

你甚至可以混合它们:

 char r; r = 82; if (r == 'R') { // will be true } 

如果您只想将ascii值保存到整数变量中

只是用这个

 int b; char c = 'r'; b = (int)c; printf("%d",b);