Skip to content
Physics and Astronomy
Hints & tips
C language
>a == b == c
XCode & the debugger
Home Our Teaching Resources C programming PHY3134 Hints & tips a == b == c
Back to top

PHY3134 Computational Physics
Hints and tips

a == b == c is not what you expect

People sometimes wish to compare three numbers for equality and try:
if ( a == b == c )
Unfortunately this doesn't do what you expect, instead it is equivalent to:
if ( (a == b) == c )
The following occurs:
  1. (a == b) is an integer expression which will evaluate to either one or zero.
  2. This value is then checked for equality with c, again producing the result one or zero.
    • Thus if c has any value other than one or zero the expression a == b == c will always evaluate to zero.
    • If c has the value one or zero the expression will evaluate to either one or zero according to whether or not a equals b.
    • Either way it won't do what you want!
Use this instead:
if ( a == b && a == c )
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top