gpt4 book ai didi

c++ - C++中char的类型(被C++入门解释搞糊涂了)

转载 作者:行者123 更新时间:2023-12-05 09:05:41 25 4
gpt4 key购买 nike

这本书是这样说的:

Unlike the other integer types, there are three distinct basic character types: char, signed char, and unsigned char. In particular, char is not the same type as signed char. Although there are three character types, there are only two representations: signed and unsigned. The (plain) char type uses one of these representations. Which of the other two character representations is equivalent to char depends on the compiler.

这个描述令人困惑。它说 char 与 signed char 不同,但随后又说它使用“两种表示之一:有符号和无符号。”

它还指出只有“2 种表示”,但实际有 3 种类型。这到底是什么意思?

最佳答案

这试图表明编译器可以自由决定他们采用哪种解释,无论是有符号还是无符号,作为程序员,您必须明白这可能不是您所期望的。如果您习惯于对其进行签名,那么另一个编译器可能是未签名的,并且您的代码可能会因为该假设而中断。

因此,如果您需要有符号或无符号,请使用适当的关键字,或者更好的是,使用类似uint8_t 的东西来明确您想要什么。

int 和其他只有在指定时才unsignedchar 在这里有点松散。

关于c++ - C++中char的类型(被C++入门解释搞糊涂了),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66882018/

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