471,073 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,073 software developers and data experts.

comparison between signed int and unsigned int

I have a problem about the comparison between signed and unsigned
integer.
See the following code:

int foo()
{
int i;
unsigned int j;
if (i < j)
{
...
}
return 0;
}

When I compile it with ccarm(a gcc version released by Wind River
designate for arm), no warning. But when I comile it with ccppc(a gcc
version released by Wind River for PowerPC), there is a warning
"warning: comparison between signed and unsigned".

Who can tell me the reason. Thank.

I also use gcc(x86, pc) test it, no warning.

Apr 26 '06 #1
3 10844
Alex wrote:
I have a problem about the comparison between signed and unsigned
integer.
See the following code:

int foo()
{
int i;
unsigned int j;
if (i < j)
{
...
}
return 0;
}

When I compile it with ccarm(a gcc version released by Wind River
designate for arm), no warning. But when I comile it with ccppc(a gcc
version released by Wind River for PowerPC), there is a warning
"warning: comparison between signed and unsigned".

Who can tell me the reason. Thank.


This is not a required diagnostic, so it is just up to the compiler
developer whether to give a warning or not. You might find that
some of the other compilers have a switch to enable or disable
this warning (gcc does).

The reason that some compilers would warn, is that signed-
unsigned comparison is a frequent source of bugs, eg.

int i = -1;
unsigned int j = 2;

if ( i > j )
puts("wasn't expecting that");

Apr 26 '06 #2
Oh, got it. thank Old Wolf.

And I have another question want to ask you:
int i;
unsigned short j;

if (i < j)
{
...
}

If j was promoted as a signed integer in the comparison? Neither ccppc
nor ccarm gave a warning.

Apr 26 '06 #3
Alex schrieb:
Oh, got it. thank Old Wolf.

And I have another question want to ask you:
int i;
unsigned short j;

if (i < j)
{
...
}

If j was promoted as a signed integer in the comparison? Neither ccppc
nor ccarm gave a warning.


If the value range of unsigned short fits completely into the
value range of int (which depends on your implementation),
the value of j indeed is subject to promotion to int.
If, for example, unsigned short has the same width and value
range as unsigned int, you are back in the original situation.
If you want to be portably on the safe side, you can change
the above to
if (i < 0 || i < j)
or even convert one or both of the operands of the second
comparison to an unsigned integer type large enough for both
values (negative values are excluded by the first test).
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Apr 26 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Aire | last post: by
18 posts views Thread by Timothee Groleau | last post: by
10 posts views Thread by =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post: by
3 posts views Thread by tcsvikr | last post: by
3 posts views Thread by john | last post: by
4 posts views Thread by Lamefif | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.