473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 11016
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: g | last post by:
Hi all. Maybe this question has been asked many times before, but I was not able to find any pointer. I apologize in advance, for it refers to a particular standard library implementation (GNU...
8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
2
by: Aire | last post by:
1. If a function defined as: void test_function(unsigned int a) { } Is "test_function(-256);" going to cause undefined behavior? 2. What's "a negative signed value wrapping"?
18
by: Timothee Groleau | last post by:
Hi all, When should I be worried about doing a comparison of signed vs unsigned ints? Could someone give me a example where such a comparison would lead to unwanted results? Thanks, Tim.
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
3
by: tcsvikr | last post by:
unsigned int _I = _N; // _N is of type unsigned int for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg && *_Pg < _I - _Np; _Grp = true)...
3
by: john | last post by:
As far as I know there is only the type wchar_t. However my compiler compiles both "signed wchar_t" and "unsigned wchar_t". Are there both signed and unsigned wchar_t types?
4
by: Lamefif | last post by:
how can the computer tell the difference between the two? i mean a byte is 8 bit can be 1 or 0 11111111 = 255 unsigned byte 10000000 = -128 or 128 ?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.