即使对char进行签名,"a"和"0"是否始终具有正值? [英] Do 'a' and '0' always have positive values even if char is signed?

查看:62
本文介绍了即使对char进行签名,"a"和"0"是否始终具有正值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取决于环境和编译器设置,类型char可以默认为带符号或无符号,这意味着8位2s补码系统上单字符常量的值范围可以为-128..127.

Depending on the environment and compiler settings, the type char can be signed or unsigned by default, which means the range of values for single character constants on 8-bit 2s complement systems can be either -128..127 or 0..255.

在普遍存在的ASCII字符集中,其ISO-8859-X扩展名或UTF-8编码,大写和小写字母以及数字的值均小于127.

In the ubiquitous ASCII character set, its ISO-8859-X extensions or the UTF-8 encoding, upper- and lowercase letters as well as digits have values below 127.

EBCDIC字符集不是这种情况:

'A'是0xC1,'a'是0x81,'1'是0xF1.

'A' is 0xC1, 'a' is 0x81 and '1' is 0xF1.

由于这些值大于127,是否表示在8位EBCDIC系统上类型char必须是无符号的?还是'a''A''1'可以具有负值?

Since these value are above 127, does it mean the type char must be unsigned on 8-bit EBCDIC systems? Or can 'a', 'A' and '1' have negative values?

其他字符集呢?字母或数字可以具有负值吗?

What about other character sets? Can the letters or digits ever have negative values?

推荐答案

C99状态

6.2.5类型

声明为char类型的对象足够大,可以存储基本执行字符集的任何成员.

An object declared as type char is large enough to store any member of the basic execution character set.

如果基本执行字符集的成员存储在char中,则其值将确保为非负数.

If a member of the basic execution character set is stored in a char its value is guaranteed to be nonnegative.

因此,如果有问题的计算机使用EBCDIC编码和8位char,则为此计算机设计的符合C99的编译器必须具有无符号char.

Thus, if the machine in question uses EBCDIC encoding and 8-bit char, then the C99 compliant compiler designed for this machine must have plain char be unsigned.

这篇关于即使对char进行签名,"a"和"0"是否始终具有正值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆