R
Richard Bos
Sona said:What is the difference between a signed 0x00 (NULL, or 0) and an
unsigned 0x00? Can there be one?
First, let's get rid of all the misconceptions.
To begin with, 0x00 is always signed. It's a hexadecimal integral
constant with the value 0, unsuffixed. This means that it is an int, and
an int is signed.
You probably meant "an object with all bits zero". Because of the
previous point, 0x00 is not a good way to express this, but I'll assume
you meant this anyway for the following points.
The second point is that NULL is a null pointer constant, not a null
pointer; the difference is important. A null pointer constant is a part
of your source code, not an object. It can be 0, or (void*)0, or
('-'-'-'), or (void *)(1 - sizeof (char) ), or anything equivalent. It
has no bit pattern as such; but in a pointer context, it is converted to
a null pointer, which has.
A null pointer, then, is not guaranteed to have the bit pattern "all
zeroes". It often does; but this is not required. All that's required is
that it is a distinct pointer that does not point at any valid object.
Comparing a null pointer to 0 works because the 0, in this pointer
context, is converted to another null pointer, not because the null
pointer is in any way compared to an integral 0.
Third, it doesn't matter. Signed integers must use the same
representation for non-negative values as their unsigned counterparts;
and this must be a pure binary representation. Moreover, all comparisons
go by value, so if you compare 0x00 with 0x00u, they must compare equal.
If I do the following:
char var;
var = 0x00;
what should var hold?
The value 0. An int with the value 0 is assigned to a char, and since
that char can hold this value, the result should be as expected.
Note that, since we don't know whether chars are signed or unsigned on
your system, we don't know whether it's a signed or unsigned zero,
either. Note also that, as above, it shouldn't matter a jot.
and if it was an unsigned char, what should it hold then?
The value 0, in an unsigned char object.
Richard