|
-
September 12th, 2010, 06:12 AM
#1
Evaluation order of expression with single AND operator?
I have an expression in C: if (aaa = bbb && (ccc = getc(stdin)) != ddd), and I need the first expression to be evaluated first. Does C, and for that matter other programming languages, always evaluate from left to right with a single boolean like this, and do the parentheses like I have on the right side mean that this side is evaluated first? Or does it depend on the compiler?
I could just separate the expressions of course, but it is more compact this way.
-
September 12th, 2010, 06:35 AM
#2
Re: Evaluation order of expression with single AND operator?
See this page
Programming languages mostly evaluate in left to right order. Parentheses group statements, causing the result contents of the parentheses to be evaluated as a single statement, but has no effect on the precedence order.
In your case this means that the expression is evaluated as
1. aaa = bbb
2. If the result of 1. is true:
3. ccc = getc(stdin)
4. result of 3. is compared to ddd
5. result of 1. is compared result of 2.
Last edited by spacewarp; September 12th, 2010 at 06:39 AM.
-
September 12th, 2010, 09:36 AM
#3
Re: Evaluation order of expression with single AND operator?
 Originally Posted by codingnewbie
I could just separate the expressions of course, but it is more compact this way.
Your code is not compact, it's crammed and that's something completely different.
Don't cram code. There's no advantage. It's not going to be any faster, just harder to understand. And if you don't easily understand your own code, chances are others won't either. At least do the assignments separately.
A good rule of thumb to avoid cramming is:
One line - one thought.
-
September 12th, 2010, 09:38 AM
#4
Re: Evaluation order of expression with single AND operator?
Actually, that is equivalent to:
Code:
if (aaa = (bbb && ((ccc = getc(stdin)) != ddd)))
Assuming that the built-in operator&& is involved here, then && introduces a sequence point. It is guaranteed that the subexpression bbb will be evaluated before ((ccc = getc(stdin)) != ddd), and that if bbb evaluates to false, ((ccc = getc(stdin)) != ddd) will not be evaluated. However, whether aaa is evaluated before bbb is implementation defined, since evaluation order is generally unspecified.
EDIT:
 Originally Posted by nuzzle
It's not going to be any faster, just harder to understand.
Agreed. spacewarp's mistake is an example of how these complex expressions can become harder to understand, even when one is fully attempting to understand it.
-
September 12th, 2010, 03:25 PM
#5
Re: Evaluation order of expression with single AND operator?
 Originally Posted by laserlight
Assuming that the built-in operator&& is involved here
It has to be, you can not overload operators in C.
Code:
if (aaa = bbb && (ccc = getc(stdin)) != ddd)
OP: That is incredibly complex, is there any way to expand that? Don't let the fact that C code doesn't get optimized fool you into thinking that expanding that a little will slow it down We have a word for code like that: obfuscated C
Last edited by ninja9578; September 12th, 2010 at 03:27 PM.
-
September 12th, 2010, 08:11 PM
#6
Re: Evaluation order of expression with single AND operator?
Thanks for all the replies. I do realize it wont make it any faster, but I actually find it easier to read this way due to it being only one line rather than two, adding another level of indentation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|