The !=
operator is more commonly used nowadays because of the overwhelming influence in C. But how did C get there?
The mathematical operator for inequality is ≠
. Some languages do use this operator directly as part of their syntax, e.g. APL or ALGOL. Since mathematical negation is ¬
, the ALGOL W dialect uses ¬=
. Whether such symbols were supported depended on the character set and keyboard of the computer system. Since many languages were defined in a more mathematical notation that didn't precisely match the actual representation of the source code, this bit of computer archeology is a bit difficult.
To write programs on systems without these symbols other graphs were used. Note that 7-bit ASCII was in no way universal for a long time, so the ASCII character set was not in itself a constraint or a reservoir of possible characters for language designers.
ALGOL supported negation as /=
which is visually very close to the mathematical operator. (As an aside, the backslash character \
was implemented so that ALGOL could write logical operators like /\
and \/
.) Many languages supported keywords that could be used in place of operators, such as .NE.
(Fortran), .NE
(6-bit ALGOL) or NE
or NEQ
.
C's ancestors are B and BCPL. These are typeless languages: everything is a machine word. They don't discern between logical and bitwise operations. In BCPL bitwise negation was ~
and pointer-dereference was !foo
. There was no ASCII-like symbol for not-equals. In B, pointer-dereference had become *foo
and negation and not-equals were !
and !=
respectively. This convention was later kept in C. I am not sure why B changed this, but B was quite shortlived and subject to much syntactic experimentation before C evolved out of it (roughly during 1971–1973).
Since then C turned out to be incredibly popular, and it is a major influence on other languages. Most newer languages see no reason to use any unexpected operators, and borrow !=
from C.
It seems that <>
was mainly used by Pascal, SQL, and BASIC since it indicates “less than or greater than”, i.e. “not equal”. However, I can't find any good references. The reports for Pascal 73 and SEQUEL 74 only seem to mention ≠
. The Pascal standard ISO 7185:1990 does use <>
but of course doesn't explain any history. I don't know what was used in the 1983 version of the standard.
Interesting documents read while researching this answer, ordered chronologically:
<>
and!=
together in SQL. Are there other languages that do it?!=
and<>
, which behave identically. (Python 3 only has!=
). Both 2 and 3 also haveis not
, which behaves differently.