473,387 Members | 1,863 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,387 software developers and data experts.

Comparing signed and unsigned values

int main()
{
bool foo=false;
for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
bar=42;
}
return 0;
}

A certain C++ compiler I am using claims that there exists in the
above code (on the line with the for loop) a comparison between a
signed and unsigned value. g++ invoked with strict options remains
silent. Is there any merit to the certain compiler's claim that
signed and unsigned values are being compared?

My guess is no, given that on

int main( void )
{
for( unsigned int bar=0; bar < (false?1:2); bar++ ) {
bar=42;
}
return 0;
}

the same compiler is silent.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 4 '05 #1
6 5134
REH

Christopher Benson-Manica wrote:
int main()
{
bool foo=false;
for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
bar=42;
}
return 0;
}

A certain C++ compiler I am using claims that there exists in the
above code (on the line with the for loop) a comparison between a
signed and unsigned value. g++ invoked with strict options remains
silent. Is there any merit to the certain compiler's claim that
signed and unsigned values are being compared?

My guess is no, given that on

int main( void )
{
for( unsigned int bar=0; bar < (false?1:2); bar++ ) {
bar=42;
}
return 0;
}

the same compiler is silent.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


1 and 2 are signed constants. Try 1u and 2u.

REH

Nov 4 '05 #2
REH <sp******@stny.rr.com> wrote:
1 and 2 are signed constants. Try 1u and 2u.


That did silence the warning, although I find it strange that it would
warn on the code I posted and never on, say,

for( unsigned int foo=0; foo < 5; foo++ );

since foo < 5 should be the same signed/unsigned comparison that it
complains about on the code I posted.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 4 '05 #3
Christopher Benson-Manica wrote:
int main()
{
bool foo=false;
for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
bar=42;
}
return 0;
}

A certain C++ compiler I am using claims that there exists in the
above code (on the line with the for loop) a comparison between a
signed and unsigned value.
That's true. The result of the 'foo?1:2' expression is signed, while 'bar' is
unsigned.
g++ invoked with strict options remains
silent.
Language specification does not define situations when warnings should (or
should not) be issued. Warnings are implementation-dependent. And they are
largely a QoI issue.
Is there any merit to the certain compiler's claim that
signed and unsigned values are being compared?
Yes. See above.
My guess is no, given that on

int main( void )
{
for( unsigned int bar=0; bar < (false?1:2); bar++ ) {
bar=42;
}
return 0;
}

the same compiler is silent.


In this case the compiler decided not to issue a waring (although technically it
could), while in the previous case it decided to issue one. It might also depend
on the compiler settings.

--
Best regards,
Andrey Tarasevich
Nov 4 '05 #4
Christopher Benson-Manica wrote:
REH <sp******@stny.rr.com> wrote:
1 and 2 are signed constants. Try 1u and 2u.


That did silence the warning, although I find it strange that it would
warn on the code I posted and never on, say,

for( unsigned int foo=0; foo < 5; foo++ );

since foo < 5 should be the same signed/unsigned comparison that it
complains about on the code I posted.


Apparently you just found the limit of that compiler's intelligence when it
comes to analyzing situations like this. In this last example the signed value
is an integral-constant expression and its value is known at compile time. The
compiler can immediately see that the value is positive and that's it is safe to
perform the comparison in unsigned context (as the language requires).

In the original case the value of the expression depended on a non-constant
boolean value, which means that the compiler could not immediately predict the
actual signed value at compile time. Of course, if it could analyze it a bit
deeper, it'd see either that 'foo' is always 'false' or that the two possible
results '1' and '2' are both positive, which means that there was no problem in
that case as well. But this compiler simply doesn't do this type of deeper
analysis. It is, once again, a pure QoI issue.

If it interests you, you might also want to try this

const bool foo=false; // <- note the 'const'
for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
bar=42;

and see what happens in this case.

--
Best regards,
Andrey Tarasevich
Nov 4 '05 #5
Andrey Tarasevich <an**************@hotmail.com> wrote:
Language specification does not define situations when warnings should (or
should not) be issued. Warnings are implementation-dependent. And they are
largely a QoI issue.


Yes, I'm aware of that; I have, however, learnt not to trust
everything this particular C++ implementation tells me.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 4 '05 #6
Andrey Tarasevich <an**************@hotmail.com> wrote:
If it interests you, you might also want to try this const bool foo=false; // <- note the 'const'
for( unsigned int bar=0; bar < (foo?1:2); bar++ ) {
bar=42; and see what happens in this case.


It did interest me, and the compiler accepted it silently. Thanks for
the insight.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 4 '05 #7

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

Similar topics

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...
10
by: tinesan | last post by:
Hello fellow C programmers, I'm just learning to program with C, and I'm wondering what the difference between signed and unsigned char is. To me there seems to be no difference, and the...
20
by: Hanzac Chen | last post by:
Hi, I don't understand why this could happen? The Code 1 will output `fff9' and the Code 2 will output `1' How could the `mod 8' not have effect? /* Code 1 */ #include <stdio.h> #include...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
3
by: Joe Van Dyk | last post by:
What bad things could happen if I compare signed and unsigned integers? (is that even valid C?) Thanks, Joe
2
by: Lugard | last post by:
Hello everyone, I am trying to optimize an application and while I was working on that I stumbled upon a question that I can't really find an answer for. I was wondering whether there was a...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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,...

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.