Questions on short circuiting, c programming?

Which parts of the expressions are not evaluated due to short circuiting?
int a = 10;
int b = -5;
int c = 15;
int flag = 1;

(a) c == b - a || !flag
(b) a != -a || !flag && c <= (-b * a)
(c) !(a % b) && b <= -5
(d) (a > 15) || c < a + b
(e) !!a == 10 && c >= 15 || b

I think the answers are:

a. Everything after the b in the equation gets short circuited.
b. Everything after the c gets short circuited.
c. Everything after the second b gets short circuited.
d. Everything after the 15.
e. Nothing.

I am not sure and am really bad at this, could some explain to me the idea and the solutions. And any errors in my thinking. Thanks.

2012-02-04T20:51:06Z

Yeah this is homework questions and it deals with order of operations. So I cannot change what is printed.

JerryOfBorg2012-02-04T21:18:24Z

Favorite Answer

The purpose of this is not only order of operations, but short circuiting is useful in that entire sections of code are not evaluated if the first part is false. This is useful because something evaluating as null may crash the program, therfore you test for it in the same statement and if something is null then you don't even get to the more complicated evaluation. Make sense?

So here are the answers.

a. Everything on the left side of the || gets evaluated. the right side is ignored. Your version is wrong because the "a" gets evaluated.
b. Everything on the right side of the && gets ignored. Your answer is wrong because the "c" is ignored as well. The expression is true because the left side is true.
c. The whole expression gets evaluated because the left half is true. Incidently the whole expression is true.
d. Everything on the right side of the || gets ignored. I am not sure here what your definition of short circuit is, so I am using "evaluated and ignored". Your answer may be wrong because the "||" is actually evaluated and is important to the result.
e. This one is a bit of a trick because "&&" gets evaluated before the "||". So, the expression evaluates to true, and it does so BEFORE we take into consideration the right side of the "||". The "b" is irrelevant and is not evaluated.

I am afraid you have scored 0%. I hope I have helped.

Anonymous2012-02-04T21:16:32Z

This may prove useful:
http://en.cppreference.com/w/cpp/language/operator_precedence

Since C++ is backwards compatible with most C code, I assume the operator precedence is the same in both languages. Just ignore the operators C doesn't have. I'm sorry I can't be of more help, but like the other poster, I've never learned or needed to know exactly which operators are evaluated when.

modulo_function2012-02-04T21:15:14Z

Without looking at the specfic question I can tell you what's involved.

Logical expressions are evaluated left to right.
Thus in:
A || B
if A is true then the expression is true regardless of the value of B. Thus many logical expressions are only evaluated as far as needed to get the final value.

In
A && B
if A is false the there's no need to know the value of B because the expression is necessarily false.

Anonymous2012-02-04T20:44:05Z

I never memorized the order of operations, but I try to use paranthesis when a statement can be made clearer with them. If your instructor is making you memorize that, instead of giving interesting assignments, he/she is a geek.

Yes I can see it deals with order of operations, that is what I said, and it deals with "short circuiting" and it deals with third grade arithmetic. Short circuiting is used all the time, but nobody in their right mind would use an expression like (a) without paranthesis unless they were a total geek.

Anonymous2016-11-06T12:24:54Z

Out of interest have you ever written in Perl. In that language any cost which couldn't 0 evaluates to real (and nil is pretend). right here there aren't any logical expressions that evaluate to real or pretend, which makes it complicated to verify what's going to extremely ensue.