473,799 Members | 3,197 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11036
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
2923
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 C++ version 3.x), but perhaps it's more general than that. The following program (to be compiled with g++ 3.x, 2.9x is not enough) #include <iostream> #include <string>
8
2247
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. The reference should be 3.9.1 (Fundamental types), and 4.7 (Integral conversions). It seems to me that the Standard doesn't specify: 1) The "value representation" of any of these types, except that (3.9.1/3) "... The range of nonnegative...
2
9117
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
9457
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
3314
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 signed/unsigned modifier to class declarations to next revision of C++ programming language
3
5047
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) //*_Pg is constant char In the above code we are comparing constant char with unsigned int(*_Pg < _I - _Np). We are getting a warning of 'warning C4018: '<' : signed/unsigned mismatch' when compiled with highest warning level in VC++. Can anyone...
3
6409
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
15273
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
9538
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.