C 中的无符号十六进制常量?

发布于 2024-10-12 19:20:18 字数 55 浏览 6 评论 0原文

C 是否将十六进制常量(例如0x23FE)视为有符号整数或无符号整数?

Does C treat hexadecimal constants (e.g. 0x23FE) as signed or unsigned integers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

帥小哥 2024-10-19 19:20:18

数字本身始终被解释为非负数。十六进制常量没有符号或任何固有的方式来表达负数。常量的类型是其中第一个可以表示其值的类型:

int
unsigned int
long int
unsigned long int
long long int
unsigned long long int

The number itself is always interpreted as a non-negative number. Hexadecimal constants don't have a sign or any inherent way to express a negative number. The type of the constant is the first one of these which can represent their value:

int
unsigned int
long int
unsigned long int
long long int
unsigned long long int
燕归巢 2024-10-19 19:20:18

它将它们视为 int 文字(基本上,作为有符号 int!)。要编写无符号文字,只需在末尾添加 u 即可:

0x23FEu

It treats them as int literals(basically, as signed int!). To write an unsigned literal just add u at the end:

0x23FEu
≈。彩虹 2024-10-19 19:20:18

根据cppreference,十六进制文字的类型是第一个类型以下列表适合该值。

int
unsigned int
long int
unsigned long int
long long int(since C99)
unsigned long long int(since C99) 

所以这取决于你的数字有多大。如果您的数字小于 INT_MAX,则它的类型为 int。如果您的数字大于 INT_MAX 但小于 UINT_MAX,则它的类型为 unsigned int,依此类推。

由于 0x23FE 小于 INT_MAX(即 0x7FFF 或更大),因此它的类型为 int

如果您希望它是无符号的,请在数字末尾添加一个 u0x23FEu

According to cppreference, the type of the hexadecimal literal is the first type in the following list in which the value can fit.

int
unsigned int
long int
unsigned long int
long long int(since C99)
unsigned long long int(since C99) 

So it depends on how big your number is. If your number is smaller than INT_MAX, then it is of type int. If your number is greater than INT_MAX but smaller than UINT_MAX, it is of type unsigned int, and so forth.

Since 0x23FE is smaller than INT_MAX(which is 0x7FFF or greater), it is of type int.

If you want it to be unsigned, add a u at the end of the number: 0x23FEu.

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