C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strange operator problem

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated)
View previous topic :: View next topic  
Author Message
dreamume
Guest





PostPosted: Thu Mar 29, 2007 6:23 am    Post subject: strange operator problem Reply with quote



Hi, everyone!
Here is my simple test code:

int main()
{
int b = 3, c = 5;
int a = ( 1 == 1 || b > c ? b : c ); // sentence 1

printf( "%d\n", a );
}

I thought the result should be 1, but is 3, if I change the sentence 1
like this,
int a = ( 1 == 0 || b > c ? b : c ); // the result is 5
or int a = ( 1 == 1 || ( b > c ? b : c ) ); // the result is 1

But I can't explain why, I guess it's a operator precedence level
problem.
Who can help me?
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
John Dallman
Guest





PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: Re: strange operator problem Reply with quote



In article <clcm-20070328-0010 (AT) plethora (DOT) net>, dreamume (AT) gmail (DOT) com
(dreamume) wrote:

Quote:
int main()
{
int b = 3, c = 5;
int a = ( 1 == 1 || b > c ? b : c ); // sentence 1

printf( "%d\n", a );
}

I thought the result should be 1, but is 3

The precedence rules mean that the compiler is reading your code as
equivalent to

int a = ( (1 == 1 || b > c) ? b : c );

Since 1==1 is true, the overall logical expressions is true at that
point, and C doesn't bother comparing b to c. Since the logical
expression is true, the conditional expression returns b.

Quote:
int a = ( 1 == 0 || b > c ? b : c ); // the result is 5

1==0 is false, so C looks at the "b > c". That's false too, so the
result of the conditional expression is c, and 5 is correct.

Quote:
int a = ( 1 == 1 || ( b > c ? b : c ) ); // the result is 1

"1==1" is logically true. "( b > c ? b : c )" returns either b or c, and
either of those, being non-zero, is logical true. So the expression is
equivalent to true OR true, which is true, and C true converted to an
int is 1.

--
John Dallman, jgd (AT) cix (DOT) co.uk, HTML mail is treated as probable spam.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Thomas Richter
Guest





PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: Re: strange operator problem Reply with quote



dreamume wrote:
Quote:
Hi, everyone!
Here is my simple test code:

int main()
{
int b = 3, c = 5;
int a = ( 1 == 1 || b > c ? b : c ); // sentence 1

printf( "%d\n", a );
}

I thought the result should be 1, but is 3, if I change the sentence 1
like this,
int a = ( 1 == 0 || b > c ? b : c ); // the result is 5
or int a = ( 1 == 1 || ( b > c ? b : c ) ); // the result is 1

The ternary operator ? binds weaker than the logical ||, so your
above expression is equivalent to:

int a = ( (1 == 1 || b > c) ? b : c );

So long,
Thomas
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Jonas
Guest





PostPosted: Mon Apr 16, 2007 8:18 pm    Post subject: Re: strange operator problem Reply with quote

"dreamume" <dreamume (AT) gmail (DOT) com> wrote in message
news:clcm-20070328-0010 (AT) plethora (DOT) net...
Quote:
Hi, everyone!
Here is my simple test code:

int main()
{
int b = 3, c = 5;
int a = ( 1 == 1 || b > c ? b : c ); // sentence 1

printf( "%d\n", a );
}

I thought the result should be 1, but is 3, if I change the sentence 1
like this,
int a = ( 1 == 0 || b > c ? b : c ); // the result is 5
or int a = ( 1 == 1 || ( b > c ? b : c ) ); // the result is 1

But I can't explain why, I guess it's a operator precedence level
problem.

Operator precedence can be found e.g. at
http://www.difranco.net/cop2220/op-prec.htm. Note that ?: has the lowest
precedence in the expression, and is right-to-left-associative.

Using parenthesis to illustrate the evaluation of

1 == 1 || b > c ? b : c

gives

(((1 == 1) || (b > c)) ? b : c)

I guess what you expected was

((1 == 1) || ((b > c) ? b : c))

Jonas
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Siddhartha Gandhi
Guest





PostPosted: Mon Apr 16, 2007 8:19 pm    Post subject: Re: strange operator problem Reply with quote

On Mar 28, 9:23 pm, "dreamume" <dream...@gmail.com> wrote:
Quote:
Hi, everyone!
Here is my simple test code:

int main()
{
int b = 3, c = 5;
int a = ( 1 == 1 || b > c ? b : c ); // sentence 1

printf( "%d\n", a );

}

I thought the result should be 1, but is 3, if I change the sentence 1
like this,
int a = ( 1 == 0 || b > c ? b : c ); // the result is 5
or int a = ( 1 == 1 || ( b > c ? b : c ) ); // the result is 1

But I can't explain why, I guess it's a operator precedence level
problem.
Who can help me?
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

int a = ( 1 == 1 || b > c ? b : c ); //original expression
int a = ( true || false ? b : c ); //simplifed
int a = ( true ? b : c ); //true OR false is true
int a = b; //so it is 3

int a = ( 1 == 0 || b > c ? b : c );
int a = ( false || false ? b : c );
int a = ( false ? b : c );
int a = c; //it is 5

int a = ( 1 == 1 || ( b > c ? b : c ) );
int a = ( 1 == 1 || ( false ? b : c ) ); //you have parantheses
int a = ( true || (c) );
int a = true; //true will become 1
int a = 1; //I'm not completely sure about this last example, but I'm
pretty certain

The ternary operator is evalulated last. First the == , then the ||
afaik.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
seni.yin
Guest





PostPosted: Mon Apr 16, 2007 8:19 pm    Post subject: Re: strange operator problem Reply with quote

you are right, iti si operator precedence level problem
a = ( 1 == 1 || b > c ? b : c ); equals a = ( (1 == 1 || b > c) ? b :
c );
because (1 == 1 || b > c) is TRUE, so a = b
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
º­×Ó(Hanzi)
Guest





PostPosted: Mon Apr 16, 2007 8:19 pm    Post subject: Re: strange operator problem Reply with quote

On Mar 29, 9:23 am, "dreamume" <dream...@gmail.com> wrote:
Quote:
Hi, everyone!
Here is my simple test code:

int main()
{
int b = 3, c = 5;
int a = ( 1 == 1 || b > c ? b : c ); // sentence 1

printf( "%d\n", a );

}

I thought the result should be 1, but is 3, if I change the sentence 1
like this,
int a = ( 1 == 0 || b > c ? b : c ); // the result is 5
or int a = ( 1 == 1 || ( b > c ? b : c ) ); // the result is 1

But I can't explain why, I guess it's a operator precedence level
problem.
Who can help me?
--
comp.lang.c.moderated - moderation address: c...@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.



In "sentence 1" a = ( 1==1 || b>c ? b : c)
Since trinary operator ?: has the lowest precedence, the expression
1==1 || b>c will be evaluated first. And because 1==1 returns 1, the
value of the expression is 1, and no judgement of b>c is processed. So
it equals to a=b, which has a value of 3.

But if you write a = (1==0 || b>c ? b : c)
The program must test whether b>c to find the value of expression 1==0
|| b>c, for 1==0 yields a 0. Now that b is 3 and c is 5, we can know
that b > c will also yield a 0. Thus 1==0 || b>c yields a 0. So that
the expression equals to a=c, which has a value of 5.

And as regards 1==1 || (b>c ? b:c)
the condition before || (i.e. 1==1) will be evaluated first. Because
it yields a 1, no judge for (b>c ? b:c) will be performed. As a
result, the value of the expression is 1, so after assignment, the
value of a is 1.
--
comp.lang.c.moderated - moderation address: clcm (AT) plethora (DOT) net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.