C语言 C中的无符号十六进制常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4737798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unsigned hexadecimal constant in C?
提问by Amr Bekhit
Does C treat hexadecimal constants (e.g. 0x23FE) and signed or unsigned int?
C 是否处理十六进制常量(例如 0x23FE)和有符号或无符号整数?
采纳答案by CB Bailey
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
回答by AraK
It treats them as intliterals(basically, as signed int!). To write an unsigned literal just add uat the end:
它将它们视为int文字(基本上,作为带符号的整数!)。要编写无符号文字,只需u在末尾添加:
0x23FEu
回答by Searene
According to cppreference, the type of the hexadecimal literal is the first type in the following list in which the value can fit.
根据cppreference,十六进制文字的类型是以下列表中可以适合该值的第一种类型。
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_MAXbut smaller than UINT_MAX, it is of type unsigned int, and so forth.
所以这取决于你的数字有多大。如果您的数字小于INT_MAX,则它的类型为int。如果您的数字大于INT_MAX但小于UINT_MAX,则它的类型为unsigned int,依此类推。
Since 0x23FEis smaller than INT_MAX(which is 0x7FFFor greater), it is of type int.
由于0x23FE小于INT_MAX(其是0x7FFF或更大),它是类型的int。
If you want it to be unsigned, add a uat the end of the number: 0x23FEu.
如果您希望它是无符号的,请u在数字末尾添加一个:0x23FEu。

