The -12
to the right of your equals sign is set up as a signed integer (probably 32 bits in size) and will have the hexadecimal value 0xFFFFFFF4
. The compiler generates code to move this signed integer into your unsigned integer x
which is also a 32 bit entity. The compiler assumes you only have a positive value to the right of the equals sign so it simply moves all 32 bits into x
. x
now has the value 0xFFFFFFF4
which is 4294967284
if interpreted as a positive number. But the printf
format of %d
says the 32 bits are to be interpreted as a signed integer so you get -12
. If you had used %u
it would have printed as 4294967284
.
In either case you don’t get what you expected since C language “trusts” the writer of code to only ask for “sensible” things. This is common in C. If you wanted to assign a value to x
and were not sure whether the value on the right side of the equals was positive you could have written unsigned int x = abs(-12);
and forced the compiler to generate code to take the absolute value of a signed integer before moving it to the unsigned integer.