The = and == conundrum

C began its life as B (ironically). B was a simplified version of BCPL, and really *that* could be where the problem began. B replaced the original “:=” and “=” of BCPL by “=” and “==” respectively, causing endless headaches. But was this really the source of the issue?

The use of “=” for assignment dates back to Heinz Rutishauser’s language Superplan, designed in the early 1950s. It was made popular by Fortran which used .EQ. as the equality operator, which left the “=” character for copying. This was somewhat problematic due to the use of “=” for equality in mathematical expressions. Algol conversely introduced the “:=” for assignment, and “=” for equality. This gels more with the idea of “:=” being associated with “is defined as”, or “becomes”, as opposed to “equals”. Following Algol, the Pascal and similar languages (Ada, Modula-2, Eiffel) continued the use of “=” for equality (and “:=” for assignment).

The replacement of “:=” with “=” as BCPL evolved into C, meant that “=” could no longer be used to test for equality. As a result, Ken Thompson (one of the architects of C) used the ad hoc “==” for equality. Now B first appeared in 1969. Interestingly, Simula 67, which appeared two years earlier used the operator “:=” for assignment, and the two operators == and =/= for “equal to” and “not equal to” respectively. It is distinctly possible that this is where the use of == in C-based languages evolved. Following C, most of its descendants also adopted the use of “==” for equality, including languages such as MATLAB, and even Fortran transitioned to “==” in Fortran90.

In 2005, Niklaus Wirth wrote in his paper Good Ideas, Through the Looking Glass:

A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957 and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let “=” denote a comparison for equality, a predicate which is either true or false. But Fortran made it to mean assignment, the enforcing of equality.

Why is it such a problem? For the, novice C programmer the similarity in the two symbols can lead to errors if the programmer misuses “=” when “==” was intended.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s