(foo == 0)

HARGED%ti-eg.csnet at CSNET-RELAY.ARPA HARGED%ti-eg.csnet at CSNET-RELAY.ARPA
Wed Jul 16 09:21:54 AEST 1986


>>      It never fails to amaze me just how many people do this:
>>              if (foo == NULL) { ... }
>>      or:
>>              if (foo == 0) { ... }
>       [instead of...]
>>              if (!foo) { ... }
>
>       ... I changed because the (!foo)
>       notation is less readable than the (foo == 0) notation.
>
>       Also, if foo is a pointer type, the test (!foo) is wrong. NULL doesn't
>       have to be zero.
>

Like you, I believe the explicit comaprisons are easier to comprehend
when browsing through code than the ubiquitous (!foo) form. In the
explicit comparison cases, generally no performance overhead is incurred
because these are optimizations that are easy to implement. I'm not sure
I'd want to work with a compiler that couldn't perform the necessary
optimizations - if it couldn't, these would cause the least of the
performance penalties you'd pay !

However, the (!foo) form is semantically valid for pointer variables. It
may force an unseen conversion in order to work correctly, but it will
work even when NULL is not represented by 0.

Richard Hargrove
CSNET: harged at ti-eg
ARPA:  harged%ti-eg.arpa at csnet-relay.arpa
-----------------------------------------



More information about the Comp.lang.c mailing list