Unsigned hexadecimal constant in C?

我与影子孤独终老i 提交于 2019-11-27 01:25:14

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

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

0x23FEu

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!