int i;
unsigned int j;
// store a value in i
j = i;
To test whether j
contains a negative number:
if (0)
{
printf("Negative!\n");
}
An unsigned integer can never hold a negative value.
If the value of i
is negative, then it will be converted to some positive value when you assign it to j
. If you want to know whether the value of i
is negative, you should test i
, not j
.
if (i < 0) {
puts("i is negative");
}
else {
puts("i is not negative");
}
puts("j is not negative");
Note that converting the value of j
back to int
is not reliable. Conversion of a value exceeding INT_MAX
to type int
yields an implementation-defined result (or raises an implementation-defined signal). It's very likely you'll get back the original value of i
, but why bother? If you want to test the value of i
, test the value of i
.