 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
dreamume Guest
|
Posted: Thu Mar 29, 2007 6:23 am Post subject: strange operator problem |
|
|
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
|
Posted: Mon Apr 16, 2007 8:18 pm Post subject: Re: strange operator problem |
|
|
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
|
Posted: Mon Apr 16, 2007 8:18 pm Post subject: Re: strange operator problem |
|
|
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
|
Posted: Mon Apr 16, 2007 8:18 pm Post subject: Re: strange operator problem |
|
|
"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
|
Posted: Mon Apr 16, 2007 8:19 pm Post subject: Re: strange operator problem |
|
|
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
|
Posted: Mon Apr 16, 2007 8:19 pm Post subject: Re: strange operator problem |
|
|
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
|
Posted: Mon Apr 16, 2007 8:19 pm Post subject: Re: strange operator problem |
|
|
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 |
|
 |
|
|
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
|
|