Set int -1 to unsigned : As -1 does not fit in the range [0...UINT_MAX] , and the multiplicity UINT_MAX+1 added until the response is in the range. Obviously, UINT_MAX on the machine is OP pow(2,32)-1 or 429496725 , so a has the value 4294967295 .
unsigned int a = -1;
The qualifier "%x" , "%u" expects an unsigned match. Because they do not match, βif the conversion specification is invalid, the behavior is undefined. If any argument does not fit the corresponding conversion specification, the behavior is undefined .β C11 Β§7.21.6.1 9. The printf specifier does not change b .
printf("%x\n", b); // UB printf("%u\n", b); // UB
The specifier "%d" expects an int match. Since they do not match, more than UB .
printf("%d\n", a);
Given undefined behavior, conclusions are not supported.
both cases, the bytes are the same (ffffffff).
Even with the same bit pattern, different types can have different values. ffffffff as unsigned has a value of 4294967295. As an int , depending on integer encoding, it has a value of -1, -2147483647 or TBD. As a float it can be NAN .
What does an unsigned word mean?
unsigned stores an integer in the range [0 ... UINT_MAX] . It never has a negative meaning. If the code needs a non-negative number, use unsigned . If the code needs a counting number, which can be +, -, or 0, use int .
Update: to avoid compiler warning about assignment of signed int to unsigned , use below. This is a negation of unsigned 1u , which is well defined as stated above. The effect is the same as -1 , but conveys the direct intent of the compiler.
unsigned int a = -1u;