472,378 Members | 1,229 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

Why does someone perfer #if defined to #ifdef

Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.

Jul 23 '05 #1
5 5647
* lovecreatesbeauty:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


#ifdef is a short form that doesn't allow more complex expressions.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
lovecreatesbeauty wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


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
makes sense...

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #3
lovecreatesbeauty wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


#ifdef is shorter form of #if defined. But #if defined offers you more
flexibility than #ifdef. e.g.

#if defined(x) || defined(y)
...
// code
#else
...
#endif

Above is not possible using #ifdef.

Krishanu

--

"Never argue with Dan Pop. And I do mean never. I think he may have
been wrong once, when he thought he was mistaken."
--Dann Corbit
Jul 23 '05 #4
On 20 Apr 2005 22:56:43 -0700, lovecreatesbeauty
<lo***************@gmail.com> wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.


The only real 'defect' is that it's a single condition being tested.
This makes it fine for header file guards:

#ifndef H_MYHEADER
#define H_MYHEADER
...
#endif

but not so useful for complex conditions:

#ifdef HAVE_MYHEADER
# if VERSION > 3
...
# endif
#endif

can often more clearly be written:

#if defined(HAVE_MYHEADER) && VERSION > 3
...
#endif

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
can see.)

Chris C
Jul 23 '05 #5
On Thu, 21 Apr 2005 06:25:55 GMT, Phlip
<ph*******@yahoo.com> wrote:
lovecreatesbeauty wrote:
Do #ifdef or #ifndef have some defects? I ever heard that some people
use #if defined() or #if !defined() instead of using #ifdef or #ifndef
in header file.
Indentifiers should be pronouncable, out-loud.


I think hrvastki is pronouncable easily ("hash ifndef" even easier, as
in "pie'n'chips"). But I have a Czech friend so I may have different
ideas about what is pronouncable <g>.
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.
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"...
So, read your code out loud, including the #ifdef, and listen for if it
makes sense...


I say a lot of code (not necessarily out loud, but I pronounce it to
myself). For instance:

x = (i > 1 ? fred : bill);

I say as "x equals if i greater than one then fred, else bill". Which
is a reason I tend to avoid very complex expressions, if I can't say it
clearly and meaningfully then it should probably be broken down into
simpler expressions.

In the case of nested #ifdefs being replaces by #if defined(...), the
same applies:

ifdef FRED then
ifdef BILL then
ifndef JOE
...

versus:

if defined FRED and defined BILL and not defined JOE ...

Chris C
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Davy | last post by:
Hello, I am using SWIG-1.3.24 to make an extension (called SHIP) to Python2.4.1 and then running under IDLE (if that makes any difference) but when I "import SHIP" I get: >>> import SHIP ...
4
by: Dave | last post by:
Is the construct #if defined MACRO_NAME part of standard C++? If so, how does it differ from #ifdef? Thanks!
0
by: fdu.xiaojf | last post by:
Hi, everyone, I'm a newbie in python. Does someone have experience about how to install python on IRIX ? I have tried several times but without success. "./configure" and "make" are both ok,...
13
by: Jenny | last post by:
Hi All, I have this code. When I type in jenny@ebay.com, it should run alert("Thanks for your interest.") line. But it does not. Why and how can I correct it? I use IE 6 with windows xp....
0
by: Bruno Ferreira | last post by:
Hi! Can someone help me? I have a Webform to register Users Traveller Expenses. To do this I created inside the webform a Custom DataSet, with a Custom DataTable (with some columns) with receives...
4
by: serge | last post by:
http://www.csharphelp.com/archives2/archive342.html I am using the sample code from this link but I am unable to figure out how to retrieve the list of the User-Defined Functions. I am able to...
7
by: jens Jensen | last post by:
I'm given an xml schema an i need to parse xml based data receive via http post and produce response . How can i parse the xml based on the .xsd file. many thanks JJ
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
19
by: liketofindoutwhy | last post by:
I did some animation using var oDiv = {}; oDiv.x = 100; oDiv.y = 100; oDiv.node = document.getElementById('divImage'); oDiv.node.style.position = "absolute"; oDiv.doAnimation = function()...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.