简体   繁体   English

为什么在if语句中对字母使用ASCII代码,而不是使用常规char数据类型?

[英]Why use ASCII code for letters of the alphabet in if statements, instead of using normal char data type?

I'm reading some lines of code written by a colleague and he has written code the following: 我正在阅读某位同事编写的一些代码行,他编写了以下代码:

int keych = 0;
keych = _getch();
if (keych == 104 || keych == 104 - 32) //Help
{
    printf(cHelp);
}

What I do'nt understand is that why does he not use simple char data type, this helps to write less code. 我不明白的是,为什么他不使用简单的char数据类型,这有助于编写更少的代码。 What are the advantages of using that method instead of char data type to get user input? 使用该方法而不是char数据类型来获取用户输入的好处是什么?

Using 使用

if (keych == 104 || keych == 104 - 32) //Help

to decide whether the input character is 'h' or 'H' is bad. 判断输入字符是'h'还是'H'是错误的。

  1. It is difficult to read. 很难阅读。
  2. It will work only on systems that use 104 and 72 for encoding h and H (such as ASCII). 它仅适用于使用104和72编码hH (例如ASCII)的系统。

It will be better to use: 最好使用:

if (keych == 'h' || keych == 'H')

or 要么

if ( tolower(keych) == 'h' )

Oups, unicode and ascii are not exactly the same thing, or more exactly ascii is a subset of unicode (127 first code points). Oup,unicode和ascii不是完全一样的东西,或更确切地说ascii是unicode的子集(127个第一个代码点)。 104 or (0x98 in hexa) is the ASCII code for 'h', so no unicode is involved here. 104或(十六进制中的0x98)是'h'的ASCII代码,因此此处不涉及unicode。 The only advantage of using ascii code is that it will break on a non ASCII system and there could still be EDCDIC system around... and additionaly as you were said in comment, 104 is more cryptic than 'h'. 使用ascii代码的唯一优点是,它将在非ASCII系统上中断,并且仍然可能存在EDCDIC系统……另外,正如您在评论中所述,104比'h'更神秘。

TL/DR: never replace characters with their ascii code point unless you have serious reason to do so. TL / DR:除非您有充分的理由,否则切勿将字符替换为其ASCII代码点。

It's good practice if not very intuitive for newbies to store character variables and pass them about as int. 对于初学者来说,存储字符变量并将其作为int传递不是很直观,这是一个好习惯。 The standard library does that, fputc() takes and int not a char. 标准库执行此操作,fputc()接受int而不是char。 In C, sizeof('a') yields 2 or 4 depending on the size of an int, not 1 for sizeof(char). 在C中,sizeof('a')根据int的大小得出2或4,而不是sizeof(char)的1。

The reason is that int can hold EOF, which is not a char and means end of input or error condition. 原因是int可以容纳EOF,这不是一个字符,表示输入结束或错误情况。 There's also the effect that the code scales to unicode quite nicely. 代码还可以很好地扩展为unicode。

The only time it's reasonable to refer to (printable) characters by their code points in an encoding is when you know that the data you are handling are not in native form for your program - this is only likely if you're implementing code conversion, either as a program like iconv or as a conversion layer in your program's i/o classes. 只有当您知道要处理的数据不是程序的本机格式时,才可以合理地用编码中的代码点引用(可打印的)字符,这只有在您实现代码转换,作为iconv类的程序,或者作为程序的I / O类中的转换层。 Normally you don't need to write such things yourself; 通常,您不需要自己编写此类内容; that's what libraries are for. 那就是图书馆的目的。

Even if you are writing conversions, it helps to give names to the values you're using, eg 即使您正在编写转换,也可以为您使用的值命名,例如

static const int UNICODE_LATIN_SMALL_LETTER_H = 0x68;

In the particular case in the question, assuming that _getch() is something like the Curses getch() , then you should expect it to be represented consistently with the character constants in your program source character set. 在问题的特殊情况下,假设_getch()类似于Curses getch() ,那么您应该期望它与程序源字符集中的字符常量一致地表示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM