0

I want to execute a multi condition if statement like below.

Head* head;
If ((head->next!=NULL)&&(head->next->next!=NULL))

The order of execution is important for the above statement to work without seg fault.

Is there a way to determine with way a compiler executes it, at compile time.

Guru
  • 29
  • 4

3 Answers3

9

The standard.

You're guaranteed the first expression will be evaluated first.

Furthermore, you're guaranteed that if the first evaluates to false, the second won't be evaluated (look up short-circuiting). If this doesn't happen, the compiler isn't compliant.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

The order is always left to right. Thus

(cond1) && (cond2)

will always end up test cond1 first, and, if that's true, cond2.

MKII
  • 892
  • 11
  • 36
1

The && and || operators force left-to-right evaluation and introduce a sequence point, so you are guaranteed that the LHS will be fully evaluated and all side effects applied before the RHS is evaluated.

John Bode
  • 119,563
  • 19
  • 122
  • 198