-5

Lets say I have the following code:

if (a&&b)
{
...
}

First question, who decides to use short-circuit evaluation in this circumstance? the compiler or the C standard?

Second question, which one will be evaluated first? a or b. The sequence is decided by whom and is there way to change the sequence in my code?

Morgan
  • 19,934
  • 8
  • 58
  • 84
henryyao
  • 1,758
  • 6
  • 23
  • 37
  • 2
    In C short-circuiting of logical expressions is guaranteed has always been a feature of C. It was true when Dennis Ritchie designed and implemented the first version of C, still true in the 1989 C standard, and remains true in the C99 standard. – Grijesh Chauhan Jul 22 '13 at 21:47

1 Answers1

7
  1. Short-circuit evaluation is required by the standard.

  2. It's always a first. You can't change that in your program.

Reference: 6.5.13 Logical AND operator, paragraph 4:

Unlike the bitwise binary & operator, the && operator guarantees left-to-right evaluation; if the second operand is evaluated, there is a sequence point between the evaluations of the first and second operands. If the first operand compares equal to 0, the second operand is not evaluated.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469