473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 5748
* 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()...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.