C 中 char 类型负值的意义

发布于 2024-08-15 08:29:51 字数 156 浏览 4 评论 0原文

  1. 'C' 中的字符用于表示字符。
  2. 所有代码页中表示字符的数字始终为正数。

有了签名字符有什么用?字符中包含的负值是否仅用作比 int 和 Short 更小的整数数据类型中的整数值?它们没有其他解释吗?(比如代表字符的 chars 中的正值)

  1. chars in 'C' are used to represent characters.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

长亭外,古道边 2024-08-22 08:29:51

'C'中的字符用于表示字符。

字符并非总是用于表示字节,它们是 c 中唯一具有已知大小的类型。

chars in 'C' are used to represent characters.

Not always, chars are used to represent bytes, they are the only type in c with a known size.

寻梦旅人 2024-08-22 08:29:51

所有代码页中表示字符的数字始终为正。

呃...错了!?

从C99标准来看,我的重点是:

如果基本执行字符集的成员存储在 char 对象中,则其值保证为正。


不保证所有代码页的所有有效字符都是正数。 char 是否有符号是由实现定义的!

Numbers representing characters in all code pages are always positive.

Erm... wrong!?

From the C99 standard, emphasis mine:

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

It is not guaranteed that all valid characters of all code page are positive. Whether char is signed or unsigned is implementation defined!

稳稳的幸福 2024-08-22 08:29:51

仅保证基本执行字符集的字符为非负(C99,6.5.2 §3):

声明为 char 类型的对象是
足够大以存储任何成员
基本执行字符集。如果
基本执行成员
字符集存储在 char 中
对象,其值保证为
非负的。如果有任何其他字符
存储在 char 对象中,结果
值是实现定义的,但是
应在数值范围内
可以用该类型表示。

您必须区分“普通”char 类型以及 signed charunsigned char 类型:signed char code> 和 unsigned char 是普通整数类型,满足以下条件(C99,6.5.2 §5):

声明为signed char类型的对象占用与a相同的存储量
“普通”字符对象。

Only characters of the basic execution character set are guaranteed to be nonnegative (C99, 6.5.2 §3):

An object declared as type char is
large enough to store any member of
the basic execution character set. If
a member of the basic execution
character set is stored in a char
object, its value is guaranteed to be
nonnegative. If any other character is
stored in a char object, the resulting
value is implementation-defined but
shall be within the range of values
that can be represented in that type.

You have to discern between the 'plain' char type and the types signed char and unsigned char as well: signed char and unsigned char are ordinary integer types for which the following holds (C99, 6.5.2 §5):

An object declared as type signed char occupies the same amount of storage as a
‘‘plain’’ char object.

千年*琉璃梦 2024-08-22 08:29:51

请注意使用普通字符作为数组索引。

char buf[10000];
fgets(buf, sizeof buf, stdin);
unsigned charcount[UCHAR_MAX] = {0};
char *p = buf;
while (*p) {
    charcount[*p]++; /* if (*p < 0) BOOM! */
    // charcount[(unsigned char)*p]++;
    p++;
}

Just beware of using plain chars as array indexes.

char buf[10000];
fgets(buf, sizeof buf, stdin);
unsigned charcount[UCHAR_MAX] = {0};
char *p = buf;
while (*p) {
    charcount[*p]++; /* if (*p < 0) BOOM! */
    // charcount[(unsigned char)*p]++;
    p++;
}
故乡的云 2024-08-22 08:29:51

来自Jack Klein 的主页

有符号字符可以保存 SCHAR_MIN 到 SCHAR_MAX 范围内的所有值(在 Limits.h 中定义)。 SCHAR_MIN 必须为 -127 或更小(负数更大),SCHAR_MAX 必须为 127 或更大。请注意,许多使用 2 的补码表示形式的处理器的编译器支持 -128 的 SCHAR_MIN,但这不是标准所要求的。

据我所知,signed char 没有正式的“含义”。然而,需要注意的一件事是所有普通 ASCII 字符都在 0-127 范围内。因此,您可以使用 signed char 类型将合法值限制在 0-127 范围内,并将任何小于 0 的值定义为错误。

例如,如果我有一个函数可以搜索某些 ASCII 文本并返回最常出现的字符,也许我可以定义一个负返回值来表示有两个或多个字符与最常出现的字符相关。这不一定是一个好方法,这只是我脑海中的一个例子。

From Jack Klein's Home Page:

Signed char can hold all values in the range of SCHAR_MIN to SCHAR_MAX, defined in limits.h. SCHAR_MIN must be -127 or less (more negative), and SCHAR_MAX must be 127 or greater. Note that many compilers for processors which use a 2's complement representation support SCHAR_MIN of -128, but this is not required by the standards.

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 the signed 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.

┈┾☆殇 2024-08-22 08:29:51

值得注意的是,char 是一种不同于有符号字符和无符号字符的类型。

It's worth noting that char is a distinct type from both signed char and unsigned char.

夜唯美灬不弃 2024-08-22 08:29:51

在 C 和 C++ 中,字符可以是有符号的,也可以是无符号的。 char 变量可用于保存小整数值。这很有用,原因如下:

  • 在小型机器上,例如 8 位微处理器。它可能允许更有效的访问和操作。
  • 如果你想要一个大的小值数组,比如 100K,你可以通过使用字符数组来节省大量内存,而不是。例如整数。

在 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:

  • On small machines, e.g. an 8-bit micro. It might allow more efficient access and manipulation.
  • If you want to have a large array of small values, say 100K, you can save a bunch of memory by using an array of chars, rather than. e.g. ints.

In C, a character literal is an integer constant. '0' is equal to 48.

梦冥 2024-08-22 08:29:51

在C语言中,char(包括signed charunsigned char)用于存储字节, C 标准定义为至少 8 位大小的小整数。

拥有有符号和无符号字节与拥有更大的整数一样有用。如果您要在数组中存储大量小数字(无符号为 0..255,有符号 [1] 为 -127..127),您可能更喜欢使用字节而不是短整数,以节省空间。

从历史上看,字节和文本字符几乎是相同的东西。然后有人意识到有比英语更多的语言。如今,文本变得更加复杂,但更改 C 中 char 类型的名称为时已晚。

[1] -128..127 对于使用二进制补码表示负数的机器,但C标准并不能保证这一点。

In C, a char (including signed char and unsigned 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文