9

Possible Duplicate:
Logical comparisons: Is left-to-right evaluation guaranteed?

I have been taught that for every C function arguments, Rightmost argument will be executed/processed first and it progresses towards left. Right part will be executed first and it progresses towards left.

Is this applicable to conditions like && and || ??

If I am writing a C++ Code, I check for NULL condtion first and then in next if I perform my action. for e.g.

 if( CommDevice != NULL)
   {
      if(CommDevice->isOpen == TRUE)
         { 
                //Do Something
         }

   }

Can I convert this in if((CommDevice != NULL) && (CommDevice->isOpen == TRUE) )

That "Code executes from Right to Left" fear is stopping me coz what if CommDevice is NULL and I am trying to access a member of NULL. It will generate exception.

Community
  • 1
  • 1
Swanand
  • 4,027
  • 10
  • 41
  • 69
  • What you're showing is expression, not argument. – shinkou Nov 07 '12 at 06:31
  • 1
    You can't count on function argument evaluation order: http://stackoverflow.com/questions/376278/parameter-evaluation-order-before-a-function-calling-in-c. That doesn't apply here, though, due to short-circuit evaluation. – chris Nov 07 '12 at 06:33
  • See [here](http://en.cppreference.com/w/cpp/language/eval_order). – juanchopanza Nov 07 '12 at 06:35

5 Answers5

10

I have been taught that every C function takes argument from right to left. Right part will be executed first and it progresses towards left.

This is 100% not true. The order of argument evaluation is unspecified!

The order of && and || is defined because it forms a sequence point. First the left is evaluated, and if not short-circuiting then the right is evaluated.

if((CommDevice != NULL) && (CommDevice->isOpen == TRUE) )

This is correct.

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • I think he meant by "takes arguments from right to left" the order in which arguments are passed, in this case, generally speaking, yes, arguments are passed in reverse order on the stack. – iabdalkader Nov 07 '12 at 06:38
  • Yes @mux, that's what I meant! – Swanand Nov 07 '12 at 06:43
  • 1
    @mux I don't see how that's relevant as the order of evaluation, not passing, is what matters. – Pubby Nov 07 '12 at 06:47
  • @Pubby what I meant is that he confused the order of evaluation with the order of passing. – iabdalkader Nov 07 '12 at 06:48
  • @mux: The mechanism for passing arguments is also unspecified. Arguments passed in registers can't be said to be ordered at all. – molbdnilo Nov 07 '12 at 06:49
  • 2
    Just one more detail: depending on the types of the arguments, it's *possible* to overload `operator&&` and/or `operator||` -- and when you do, it's a normal function call, the order of evaluation is unspecified. Needless to say, such overloads are rarely a good idea. – Jerry Coffin Nov 07 '12 at 06:51
  • 1
    @molbdnilo that's why I said "generally speaking" I wanted to add that it depends on the calling convention, but the point is I wanted to clear that for the OP. – iabdalkader Nov 07 '12 at 06:51
  • @mux: I do not think he was confused. gcc for example evaluates from right to left. However it's formally unspecified and they could change the behavior at any time. – Matthieu M. Nov 07 '12 at 07:42
  • @MatthieuM. The OP said so himself, check the second comment. I know it's unspecified, that's not the point, I'm just saying he mixed passing arguments with evaluating arguments. – iabdalkader Nov 07 '12 at 07:58
  • @mux: *Right part will be executed first and it progresses towards left.* => I think he clearly stated what gcc does. Though I suppose it does not matter so much either. – Matthieu M. Nov 07 '12 at 10:19
  • Relax guys! @mux You are right. I am updating question as @ Matthieu M. suggested. – Swanand Nov 07 '12 at 19:32
3

From the "C Programming Language" by Brian Kernighan & Ritchie Dennis (authors of C):

  • "Expressions connected by && or || are evaluated left to right, and it is guaranteed that evaluation will stop as soon as the truth or falsehood is known."

I should point out that I once confused the order of evaluation rules for && and || with the following in C:

x = f() + g();

Note that order of evaluation for f() vs. g() CANNOT be determined.

sampson-chen
  • 45,805
  • 12
  • 84
  • 81
0

&& evaluates from left to right, not right to left! So your code will NOT throw any exception.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

As far as the logical operators are concerned, you're touching on short-circuit evaluation.

It is safe to check for the NULL pointer first on the left, because if this condition returns false, then the next condition of the && (from left to right) is not evaluated.

This behavior is allowed because of the basic logical principles of && and ||.

kevintodisco
  • 5,061
  • 1
  • 22
  • 28
0

Order of evaluation of functions arguments is Unspecified .So whatever you have been taught is not according to language standard.

Regarding your code :

The order of evaluation of && and || is specified and this is Left to right

And this also follows the short-circuit of statement In case of && if 1st is false then rest of the conditions will not be executed. Similar in || if 1st is true then rest of the conditions will not be executed.

And also && and || are sequence points.

Omkant
  • 9,018
  • 8
  • 39
  • 59