Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsigned hexadecimal constant in C?

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

like image 441
Amr Bekhit Avatar asked Jan 19 '11 16:01

Amr Bekhit


People also ask

What is hexadecimal constant in C?

If an integer constant begins with 0x or 0X, it is hexadecimal. If it begins with the digit 0, it is octal. Otherwise, it is assumed to be decimal.

What are hexadecimal constants?

A hexadecimal constant is an alternative way to represent numeric constants. A hexadecimal constant takes one of the following forms: Z'd [d...]' Is a hexadecimal (base 16) digit (0 through 9, or an uppercase or lowercase letter in the range of A to F).

What is an unsigned integer constant?

Unsigned integer constant is an integer constant which has the permissible range from 0 to 65536. Thus significance of declaring a constant as unsigned almost doubles the size of the largest possible value.


2 Answers

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 
like image 78
CB Bailey Avatar answered Oct 04 '22 21:10

CB Bailey


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

0x23FEu 
like image 35
Khaled Alshaya Avatar answered Oct 04 '22 20:10

Khaled Alshaya