gpt4 book ai didi

c - 宽字符的意外值

转载 作者:行者123 更新时间:2023-11-30 14:59:25 27 4
gpt4 key购买 nike

给定这个程序,它会打印-43632 1。预计第二个值为 1,但我不明白它是如何计算负值的。

ISO9899 标准中的何处解释了如何计算此值?

#include <stdio.h>
int x = L'\uaaaa9', y = L'\uaaaa';

main()
{
printf("%d %d\n", x-0xaaa9, y-0xaaa9);
}

编辑:编写这个荒谬的程序时,我试图理解由多个字符组成的常量字符何时有效,因为在标准中,它在语言的语法中是允许的(有关 BNF 的摘要,请参阅附录 A)单个常量字符中的多个字符。

最佳答案

标准中的位置 - 无处可去。您的程序不严格符合:

% gcc test.c -Wall -Werror -pedantic -std=c11
test.c:2:9: error: character constant too long for its type [-Werror]
int x = L'\uaaaa9', y = L'\uaaaa';

L'\uaaaa' 是一个字符后跟另一个字符 L'9'

C11 6.4.4.4p11说:

The value of a wide character constant containing more than one multibyte character or a single multibyte character that maps to multiple members of the extended execution character set, or containing a multibyte character or escape sequence not represented in the extended execution character set, is implementation-defined.

The behaviour of GCC in the case of integer character constants is :

The compiler evaluates a multi-character character constant a character at a time, shifting the previous value left by the number of bits per target character, and then or-ing in the bit-pattern of the new character truncated to the width of a target character. The final bit-pattern is given type int, and is therefore signed, regardless of whether single characters are signed or not. If there are more characters in the constant than would fit in the target int the compiler issues a warning, and the excess leading characters are ignored.

然而,随着我对标准的阅读越多,我感觉 GCC 没有正确记录多字符宽字符常量的情况,因为文档只是提到类型的字符常量int,但 L'' 常量应生成值 wchar_t

无论如何,您看到的值 -43632 来自 L'\uaaaa9' 的值为 L'9' 在您的平台上,即 (wchar_t)0x39;和 )0x39 - 0xaaa9 将导致 -43632。

<小时/>

总而言之,永远不要依赖多字符宽常量来产生任何有意义的内容,因为标准并不支持它。多字符整数常量也值得怀疑,因为虽然它们可能有用,但它们的值仍然是实现定义的,并且并非所有实现都同意。

关于c - 宽字符的意外值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42749563/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com