!x vs x == 0
!x vs x == 0
Posted Jul 16, 2004 23:50 UTC (Fri) by giraffedata (guest, #1954)In reply to: NULL v. zero by jabcslwn
Parent article: NULL v. zero
I believe C was designed to allow "if (!a)" for the case that a is a logical (boolean) quantity. That it can be used with pointers is a byproduct. I believe the concept of the null pointer came later.
However, I accept "if (!a)" where a is a pointer, because a pointer which may have the null pointer value is in fact two pieces of information in one, and one of them is logical. a having a non-null-pointer value is the logical proposition that a exists. Looked at that way, "if (a == NULL)" is actually harder to read, and not because it uses a few more characters. Because it presents the absence-of-value NULL as if it were an actual pointer value.
Of course, one thing that will always raise my hackles is "if (!a)" where a is a number.