``='' versus ``==''

When you write if statements in C, there is a very important ``catch'' that you need to watch out for. It is very important that you remember to use ``=='' when to want know if two numeric values are equal. Why is it so important? Because it's easy to write this:

      if (a = b) ...

with only one ``='' sign, when you really mean to write this:

      if (a == b) ...

What's wrong with writing ``if (a = b)''?

Click here for the answer.

Suppose that you are writing a program and need to know if the variable delta is equal to zero. If you write this:

      if (delta = 0.0) ...

what do you think will happen?

Click here for the answer.

The moral is to never confuse the ``='' assignment operator with the ``=='' comparison operator. Because it is so easy to mix up these operators in if statements, some C compilers will warn you whenever you use ``='' inside an if condition. But not all compilers are so smart.


Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah