473,396 Members | 1,938 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.

comparing signed and unsigned integers

What bad things could happen if I compare signed and unsigned integers?
(is that even valid C?)

Thanks,
Joe
Jun 23 '06 #1
3 3822
Joe Van Dyk wrote:
What bad things could happen if I compare signed and unsigned integers?
(is that even valid C?)


Second question first: Yes, it is valid. The operands are
subject to the "usual arithmetic conversions" (6.3.1.8), which
reconcile them to a common type. The comparison is then carried
out in that common type.

First question second: The usual arithmetic conversions may
result in a type that is surprising to the novice, changing the
"obvious" meaning of the expression:

int i = -1;
unsigned int u = i + 1;
if (u > i) ...

Clearly, u is initialized to a value greater than that of i:
after all, i is negative and u is zero, so u must be greater
than i, right? Not so, Grasshopper: after the "usual arithmetic
conversions" (which in this case convert both operands to
unsigned int), the converted i will be UINT_MAX, much *larger*
than u. If you don't understand what's happening, the result
might be a "bad thing."

--
Eric Sosman
es*****@acm-dot-org.invalid

Jun 23 '06 #2
Joe Van Dyk wrote:
What bad things could happen if I compare signed and unsigned integers?
(is that even valid C?)

Thanks,
Joe


Besides nasal demons? :-)

First, why would you mix signed and unsigned int values for comparison?

These are two different animals and generally shouldn't be compared.

But if you must, check the int for < 0. If negative, there's your answer
because unsigned is >= 0 by definition.

Now, if the int is positive, cast it to unsigned and do the comparison.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 25 '06 #3

Joe Wright wrote:
Joe Van Dyk wrote:
What bad things could happen if I compare signed and unsigned integers?
(is that even valid C?)

Thanks,
Joe


Besides nasal demons? :-)

First, why would you mix signed and unsigned int values for comparison?

These are two different animals and generally shouldn't be compared.

But if you must, check the int for < 0. If negative, there's your answer
because unsigned is >= 0 by definition.

Now, if the int is positive, cast it to unsigned and do the comparison.


My usual solution is to actually review the code. If I know the value
never has to be tested for >= 0 [e.g. decrementing loop] or negative I
convert it to unsigned locally.

Otherwise, I look at the values and use good common sense... e.,g.

int x;
unsigned char myarray[100];
for (x = 0; x < sizeof(myarray); x++) { myarray[x] = 0; }

That will generate the diagnostic with GCC. The fix I use (if I can't
move x to unsigned) is to just realize that myarray is less than the
minimum size for an int and cast the sizeof. e.g.

for (x = 0; x < (int)sizeof(myarray); x++) { ... }

I don't do the cast to "fix" the code, it's just my way of
acknowledging the diagnostic and signalling to myself that I've
addressed the issue.

Tom

Jun 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: MiniDisc_2k2 | last post by:
Okay, here's a question about the standard. What does it say about unsigned/signed mismatches in a comparison statement: char a = 3; unsigned char b = 255; if (a<b) Now what's the real...
44
by: JKop | last post by:
You know how the saying goes that *unsigned* overflow is... well.. defined. That means that if you add 1 to its maximum value, then you know exactly what value it will have afterward on all...
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....
16
by: Kevin Goodsell | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsigned value to a signed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
89
by: purifier | last post by:
The problem is to write a program in 'C' to find the greatest of 2 given numbers... Easy? huh here's the catch do not use 'if' or any conditional statements if u want it to be a little more...
3
by: Bilgehan.Balban | last post by:
Hi, I have a basic question on signed and unsigned integers. Consider the following code: #define SOME_ADDR 0x10000000 // Some context { unsigned int *x = (unsigned int *)(SOME_ADDR);
11
by: Frederick Gotham | last post by:
I'd like to discuss the use of signed integers types where unsigned integers types would suffice. A common example would be: #include <cassert> #include <cstddef> int...
39
by: Juha Nieminen | last post by:
I was once taught that if some integral value can never have negative values, it's a good style to use an 'unsigned' type for that: It's informative, self-documenting, and you are not wasting half...
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...
0
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,...

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.