-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,186
  • 6
  • 57
  • 81
henryyao
  • 1,658
  • 6
  • 22
  • 36
  • 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
  • 210,715
  • 34
  • 410
  • 462