Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a negative to "%hu" in C

Why does this code print 65535? :

printf( "%hu" ,  ‐1 );
like image 971
Александар Стјепановић Avatar asked Oct 16 '25 17:10

Александар Стјепановић


1 Answers

It will print 65535

"%hu" is an unsigned short int which is 16 bit.

-1 is "all-ones", e.g. 0xffff.ffff, but since it gets converted to short it is only 0xffff. Which is 65535 as unsigned.

like image 177
Support Ukraine Avatar answered Oct 18 '25 06:10

Support Ukraine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!