unsigned intiger is storing signed value !!!

machine specs
gcc version 3.4.3
SunOS 5.10 Generic_118833-24 sun4v sparc SUNW,Sun-Fire-T200
---------------------------
/* code */
unsigned int a=3;
a=a-4;
printf("%02d",a);
if (a<0)
printf("%02d",a);
/* end code */
output
-1
----------------------------
my question is; if a is an unsigned variable how is it storing a -ve value
secondly, if its storing the -ve value why is the check failing (a<0)
I should get -1 two times not once.

Thanks,
A negative number is stored as the two's compliment.

unsigned a = 3; a=a-4; is an underflow condition and is not checked by the processor. It's your respnsibilty to resolve.

a<0 is always false for an unsigned quantity, as an unsigned number cannot be less than zero.

If you want printf to show you the value of an unsigned number you should use printf("%u", a);
usually unsigned n = -1; returns std::numeric_limits<unsigned>::max();
Topic archived. No new replies allowed.