Why is 0XAA an unsigned int and not an int?

I saw this from C Primer Plus, sixth edition, reviewing questions in chapter 3.

Question:

Question Picture

Answer in Appendix A:

Answer Picture

Pay attention to d.0XAA, my answer is int constant, hexadecimal, but the answer is unsigned int

and I wonder why

+5
source share
1 answer

This book is incorrect. According to C11 6.4.4.1, the type of the integer constants of the hexadecimal value is determined from this table:

 Suffix ... Octal or Hexadecimal Constant None ... int unsigned int long int unsigned long int long long int unsigned long long int u or U ... unsigned int unsigned long int unsigned long long int 

Your 0xAA constant 0xAA not have a suffix, so the top of the table above applies. Value: the compiler first checks to see if the value matches in int . If it does not fit, it checks to see if it matches unsigned int , etc.

In any known C implementation, the value 0xAA will certainly match inside an int . The correct answer to the question is: int .

However, if the constant were 0xAAu , the bottom of the cited table would be applied, and the result would be unsigned int .

+5
source

Source: https://habr.com/ru/post/1259965/


All Articles