hari wrote:
> Hi,
>
> #include <stdio.h>
> #include<conio.h>
> int main()
> {
> int x= 1;
> unsigned int y = x ;
> unsigned int j = y;
>
> printf("%x %x",x,y)
> return 0;
> }
>
> When I execute this,I' m getting the output as 0xfffffff
Are you sure that wasn't 0xffffffff? A value of 0xfffffff is technically
possible, but extremely unlikely unless you're using a very unusual machine.
> 0xfffffff.My expectation is , y is unsigned int,so on assigning a
> signed variable with value 1.It should go to 0.
>
> Please correct me if I' m wrong .
If an integer value is outside the valid range for an unsigned integer
type, when you convert that value to that type, the result is determined
using modulus arithmetic. This means that you first determine the number
that is 1 larger than the largest value that can be stored in in the
unsigned type; in your case, that would be UINT_MAX+1, which is probably
0x100000000 (assuming that I was right about you miscounting the number
of 'f's). Then you repeatedly either add or subtract this number from
the value, until you get a result that is in range for the unsigned
type. In this case, a single addition of UINT_MAX+1 to 1 gives you a
value of UINT_MAX, which is within range, so that's the result you
should be getting.
