Unsigned hexadecimal constant in C?

前端 未结 3 2101
难免孤独
难免孤独 2020-11-27 18:07

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

3条回答
  •  [愿得一人]
    2020-11-27 18:53

    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.

提交回复
热议问题