0

Can anyone explain what is the benefit of comparing constant and variable with constant on the left e.g.

if (0 == variable)

Instead of

if (variable == 0)
fedorqui
  • 275,237
  • 103
  • 548
  • 598
sotona
  • 1,731
  • 2
  • 24
  • 34
  • 1
    To avoid accidental assignment, `if (variable = 0)`. If the constant is on the left-hand side, then it would be a syntax error: `if (0 = variable)` – Alex Shesterov Apr 15 '16 at 21:07
  • Is it a coincidence that your avatar is Yoda? Anyway, compile with `-Wall` (or the equivalent for your compiler) and don't bother with this awful convention. – Fred Larson Apr 15 '16 at 21:16

1 Answers1

0

If you say

if (variable == 0)

you are under the risk of omiting one = and saying

if (variable = 0)

which will set the variable variable to 0.

So putting 0 to the left makes it more typo-safe, because 0 = variable won't be able to set any value to the variable.

fedorqui
  • 275,237
  • 103
  • 548
  • 598