简体   繁体   English

为什么char比较“if(c> ='a'&& c <='z')”不可移植?

[英]why the char comparison “if(c>='a' && c<='z')” is not portable?

Some one told me that the char comparison if(c>='a' && c<='z') is not portable. 有人告诉我,char比较if(c>='a' && c<='z')是不可移植的。

example

int main() {
    char c;
    scanf("%c", &c);
    if(c>='a' && c<='z')
        printf("lower case\n");
}

Any proof that the char comparison if(c>='a' && c<='z') is not portable? 任何证明char比较if(c>='a' && c<='z')是不可移植的?

The C standard does not guarantee that only lower case letters appear between 'a' and 'z' in the execution character set. C标准不保证在执行字符集中只有小写字母出现在'a''z'之间。

In the EBCDIC encoding, there are other characters between 'a' and 'z' , and some C implementations use EBCDIC. 在EBCDIC编码中, 'a''z'之间还有其他字符,而某些C实现使用EBCDIC。

The C standard does guarantee that the digits are consecutive, so '0' <= d && d <= '9' does test whether d is a decimal digit character. C标准确保数字是连续的,因此'0' <= d && d <= '9'确实测试d是否是十进制数字字符。

Any proof that the char comparison if(c>='a' && c<='z')) is not portable? 任何证明char比较if(c> ='a'&& c <='z'))是不可移植的?

c = '~';

if  (c>='a' && c<='z'))
{
   printf("Yes\n");
}

will print Yes in a AS/400 from IBM because in EBCDIC encoding ~ is between a and z . 将打印Yes因为在EBCDIC编码在AS / 400从IBM ~之间az

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

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