C 中 char 类型负值的意义
- 'C' 中的字符用于表示字符。
- 所有代码页中表示字符的数字始终为正数。
有了签名字符有什么用?字符中包含的负值是否仅用作比 int 和 Short 更小的整数数据类型中的整数值?它们没有其他解释吗?(比如代表字符的 chars 中的正值)
- chars in 'C' are used to represent characters.
- Numbers representing characters in all code pages are always positive.
What is the use of having signed characters?? Are negative values contained in chars used only as integral values in a smaller integral data-type than int and short?? Do they have no other interpretation??(like positive values in chars representing characters)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
字符并非总是用于表示字节,它们是 c 中唯一具有已知大小的类型。
Not always, chars are used to represent bytes, they are the only type in c with a known size.
呃...错了!?
从C99标准来看,我的重点是:
不保证所有代码页的所有有效字符都是正数。
char
是否有符号是由实现定义的!Erm... wrong!?
From the C99 standard, emphasis mine:
It is not guaranteed that all valid characters of all code page are positive. Whether
char
is signed or unsigned is implementation defined!仅保证基本执行字符集的字符为非负(C99,6.5.2 §3):
您必须区分“普通”
char
类型以及signed char
和unsigned char
类型:signed char
code> 和unsigned char
是普通整数类型,满足以下条件(C99,6.5.2 §5):Only characters of the basic execution character set are guaranteed to be nonnegative (C99, 6.5.2 §3):
You have to discern between the 'plain'
char
type and the typessigned char
andunsigned char
as well:signed char
andunsigned char
are ordinary integer types for which the following holds (C99, 6.5.2 §5):请注意使用普通字符作为数组索引。
Just beware of using plain chars as array indexes.
来自Jack Klein 的主页:
据我所知,
signed char
没有正式的“含义”。然而,需要注意的一件事是所有普通 ASCII 字符都在 0-127 范围内。因此,您可以使用signed char
类型将合法值限制在 0-127 范围内,并将任何小于 0 的值定义为错误。例如,如果我有一个函数可以搜索某些 ASCII 文本并返回最常出现的字符,也许我可以定义一个负返回值来表示有两个或多个字符与最常出现的字符相关。这不一定是一个好方法,这只是我脑海中的一个例子。
From Jack Klein's Home Page:
From what I can tell, there's no official "meaning" of
signed char
. However, one thing to be aware of is that all the normal ASCII characters fall in the 0-127 range. Therefore, you can use thesigned char
type to restrict legal values to the 0-127 range, and define anything less than 0 as an error.For example, if I had a function that searches some ASCII text and returns the most frequently occurring character, perhaps I might define a negative return value to mean that there are two or more characters tied for most frequent. This isn't necessarily a good way to do things, it's just an example off the top of my head.
值得注意的是,char 是一种不同于有符号字符和无符号字符的类型。
It's worth noting that char is a distinct type from both signed char and unsigned char.
在 C 和 C++ 中,字符可以是有符号的,也可以是无符号的。 char 变量可用于保存小整数值。这很有用,原因如下:
在 C 中,字符文字是整型常量。 “0”等于 48。
In C and C++ chars can be signed or unsigned. A char variable can be used to hold a small integer value. This is useful for several reasons:
In C, a character literal is an integer constant. '0' is equal to 48.
在C语言中,
char
(包括signed char
和unsigned char
)用于存储字节, C 标准定义为至少 8 位大小的小整数。拥有有符号和无符号字节与拥有更大的整数一样有用。如果您要在数组中存储大量小数字(无符号为 0..255,有符号 [1] 为 -127..127),您可能更喜欢使用字节而不是短整数,以节省空间。
从历史上看,字节和文本字符几乎是相同的东西。然后有人意识到有比英语更多的语言。如今,文本变得更加复杂,但更改 C 中 char 类型的名称为时已晚。
[1] -128..127 对于使用二进制补码表示负数的机器,但C标准并不能保证这一点。
In C, a
char
(includingsigned char
andunsigned char
) is used to store a byte, which the C standard defines as a small integer at least 8 bits in size.Having signed and unsigned bytes is as useful as having larger integers. If you're storing a very large number of small numbers (0..255 for unsigned, -127..127 for signed[1]) in an array, you may prefer to use bytes for them rather than, say, short ints, to save space.
Historically, a byte and a text character were pretty much the same thing. Then someone realized there are more languages than English. These days, text is much more complicated, but it is too late to change the name of the
char
type in C.[1] -128..127 for machines with two's complement representation for negative numbers, but the C standard does not guarantee that.