如何比较ASCII值

XII*_*IIX 2 c text ascii file file-read

我想将字母的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)
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

另外还要注意,我怎样才能.txt逐个字符地读取文件?所以它可以保存在字符变量上.

Pab*_*ruz 11

做就是了:

if (r == 82) {
   // provided r is a char or int variable
}
Run Code Online (Sandbox Code Playgroud)

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

char r;
r = 82;
if (r == 82) {
}
Run Code Online (Sandbox Code Playgroud)

是相同的:

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

} 
Run Code Online (Sandbox Code Playgroud)

你甚至可以混合它们:

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

}
Run Code Online (Sandbox Code Playgroud)

  • 在C中,变量不是_necessarily_ ASCII,只有大约99.9%的机器出现这是真的,不幸的是,我在其他一个上工作:-) (3认同)