473,568 Members | 2,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5765
* lovecreatesbeau ty:
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
lovecreatesbeau ty 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
lovecreatesbeau ty 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, lovecreatesbeau ty
<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_MY HEADER) && 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*******@yaho o.com> wrote:
lovecreatesbeau ty 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'chip s"). 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
2748
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 Traceback (most recent call last): File "<pyshell#0>", line 1, in -toplevel-
4
55480
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
995
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, but got errors when I run "make test".
13
1968
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. Thanks a lot. <HTML><body> <form name="myForm"> <INPUT TYPE="text" VALUE="Enter email" NAME="userEmail"
0
1190
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 Custom DataRows. In my webform I let the User Fill the textboxes and when he push the add button a row is added to my DataSet and finaly a Bind...
4
5820
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 get the count of the user defined functions correctly using: db.ListObjects(SQLDMO.SQLDMO_OBJECT_TYPE.SQLDMOObj_UserDefinedFunction,...
7
2334
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
5968
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 used." Could anybody tell me why gets() function is dangerous?? Thank you very much. Cuthbert
19
1826
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() {
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.