Why does someone perfer #if defined to #ifdef
Monday, November 1, 2010 2:43:56 AM
Here is the correct and formal answer:
#ifdef is a short form that doesn't allow more complex expressions.
The only real 'defect' is that it's a single condition being tested.
This makes it fine for header file guards:
but not so useful for complex conditions:
# if VERSION > 3
can often more clearly be written:
#if defined(HAVE_MYHEADER) && VERSION > 3
It's largely a matter of style preference. I like the #ifndef for
header include guards because it's easy to see that the macro being
tested is immediately defined, but for complex conditions I generally
prefer using defined().
(Whether you do defined FRED or defined(FRED) is also a style issue,
I've worked in places where one is compulsory and the other forbidden
but there is no logical reason to prefer one to the other as far as I
But I like another answer that's quite interesting.
Also someone mentioned how *www* are pronounced in other languages:
Indentifiers should be pronouncable, out-loud.
For example, someone had the brainless idea to prefix internet servers that
serve HTTP with 'www'. Nobody reviewed that prefix for pronouncability,
meaning today radio personalities suffer when their programming formats
forbid "dub-dub-dub" or "triple-double-you". They must say "double-you
double-you double-you" all day.
So, read your code out loud, including the #ifdef, and listen for if it
It's easy in German: "vay vay vay". Not too bad in Cymraeg (Welsh): "oo
oo oo". In English I've heard "wuh wuh wuh"...