Francis J. A. Pinteric
linuxdoctor at linux.ca
Tue Jan 28 11:26:31 EST 2003
On Tue, 28 Jan 2003 10:55:52 -0500 (EST)
"Rod Giffin" <rod at giffinscientific.com> wrote:
> Shad Young said:
> > warning: comparison between signed and unsigned integer expressions
> > Why is this bad?
> It's not necessarily bad, which is why it's only a warning instead of
> an error. The warning is just to draw your attention to something
> that might be a logic error.
There's another reason as well. Signed integers are, as the name
implies, signed. They have a positive and negative value, while unsigned
integers have only positive values. However, they share the same number
of bits, although the hardware representations of the two are different.
For 16-bit signed integers, the values range from -32768 to +32767. For
the same sized unsigned integers, the value is in the range of 0 to
65535. By mixing the two types together you can get confusing results.
For instance, take the following C programme:
unsigned short y, z;
x = -10;
y = 15;
z = x;
if( y > z )
printf("y > z\n");
printf("y < zx\n");
What will happen? Compile it and find out. The result may surprise you.
That's the problem of assigning signed variables to unsigned variables
and vice versa.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 189 bytes
Desc: not available
Url : http://tux.oclug.on.ca/pipermail/oclug/attachments/20030128/ba87b7ca/attachment.bin
More information about the OCLUG