I have the following code:
#include <stdio.h> int main() { unsigned int a = -1; int b = -1; printf("%x\n", a); printf("%x\n", b); printf("%d\n", a); printf("%d\n", b); printf("%u\n", a); printf("%u\n", b); return 0; }
The output is:
ffffffff ffffffff -1 -1 4294967295 4294967295
I can see that a value is interpreted as signed or unsigned according to the value passed to printf
function. In both cases, the bytes are the same (ffffffff
). Then, what is the unsigned
word for?
An unsigned integer is a 32-bit datum that encodes a nonnegative integer in the range [0 to 4294967295]. The signed integer is represented in twos complement notation. The most significant byte is 0 and the least significant is 3.
The term "unsigned" in computer programming indicates a variable that can hold only positive numbers. The term "signed" in computer code indicates that a variable can hold negative and positive values. The property can be applied to most of the numeric data types including int, char, short and long.
unsigned char is a character datatype where the variable consumes all the 8 bits of the memory and there is no sign bit (which is there in signed char). So it means that the range of unsigned char data type ranges from 0 to 255.
Since there is no sign bit in this unsigned binary number, so N bit binary number represent its magnitude only. Zero (0) is also unsigned number.
Assign a int -1
to an unsigned
: As -1
does not fit in the range [0...UINT_MAX]
, multiples of UINT_MAX+1
are added until the answer is in range. Evidently UINT_MAX
is pow(2,32)-1 or 429496725
on OP's machine so a
has the value of 4294967295.
unsigned int a = -1;
The "%x"
, "%u"
specifier expects a matching unsigned
. Since these do not match, "If a conversion specification is invalid, the behavior is undefined. If any argument is not the correct type for 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 "%d"
specifier expects a matching int
. Since these do not match, more UB.
printf("%d\n", a); // UB
Given undefined behavior, the conclusions are not supported.
both cases, the bytes are the same (ffffffff).
Even with the same bit pattern, different types may have different values. ffffffff
as an unsigned
has the value of 4294967295. As an int
, depending signed integer encoding, it has the value of -1, -2147483647 or TBD. As a float
it may be a NAN.
what is unsigned word for?
unsigned
stores a whole number in the range [0 ... UINT_MAX]
. It never has a negative value. If code needs a non-negative number, use unsigned
. If code needs a counting number that may be +, - or 0, use int
.
Update: to avoid a compiler warning about assigning a signed int
to unsigned
, use the below. This is an unsigned
1u
being negated - which is well defined as above. The effect is the same as a -1
, but conveys to the compiler direct intentions.
unsigned int a = -1u;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With