472,331 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

#ifdef vs #if vs #if defined

Which is the preferred method for preprocessor tests and why?

#ifdef XYZ
or
#if XYZ
or
#if defined(XYZ)

and

#elif
or
#elsif

and

#ifndef XYZ
or
#if !defined(XYZ)

Mike
Nov 14 '05 #1
6 73816


Michael B Allen wrote:
Which is the preferred method for preprocessor tests and why?

#ifdef XYZ
or
#if XYZ
or
#if defined(XYZ)
The first and third are equivalent; use whichever
you please. The second is slighly different from the
other two, since it will test "false" if XYZ is
defined but with the value zero.
and

#elif
or
#elsif
The first is strongly preferred, except by people
who enjoy reading error messages.
and

#ifndef XYZ
or
#if !defined(XYZ)


Equivalent; use whichever you like.

--
Er*********@sun.com

Nov 14 '05 #2
In article <pa********************************@ioplex.com>,
Michael B Allen <mb*****@ioplex.com> wrote:
:Which is the preferred method for preprocessor tests and why?

YMMV, but:

- expression format is the easiest to change later.
- you are likely going to need to use expression format for some of
what you do. It gets to look odd if you deliberately avoid expression
format when possible.

:#ifdef XYZ
:or
:#if XYZ
:or
:#if defined(XYZ)

The second of those does not have the same meaning as the other two.
If XYZ is defined as 0, then #ifdef and #if defined are both true
but the #if XYZ form is false.
--
Pity the poor electron, floating around minding its own business for
billions of years; and then suddenly Bam!! -- annihilated just so
you could read this posting.
Nov 14 '05 #3
Michael B Allen wrote:
Which is the preferred method for preprocessor tests and why?
#ifdef XYZ
or
#if XYZ
or
#if defined(XYZ)
"#ifdef XYZ" is different from "#if XYZ", the former is testing the
existence of XYZ while the latter tests the value of XYZ.
IMO, you can freely choose between "#ifdef XYZ" and "#if defined(XYZ)", with
one exception, you cannot test for more than one thing in a single
"#ifdef".
consider the following, it's better to use "#if defined" than "#ifdef":

/* Test for the support of C99 Standard */
#if defined(__STDC_VERSION__) && __STD_VERSION__ > 199901L
/* ...something legal in C99 only */
#elif
/* ...something alternative */
#endif

#elif
or
#elsif
I can't remember I saw something like #elsif, is it a compiler-specific
extension? (okay, I might saw it when I used Turob C 2.0, but that's long
time ago, 10+ years, I am not quite sure).
by the way, #elsif is not a standard c preprocessor feature, avoid using it.
and
#ifndef XYZ
or
#if !defined(XYZ)
Mike

What's true for "#ifdef" and "#if" is also true here.
--
Java is not platform-independent, it is the platform!
Nov 14 '05 #4
Walter Roberson wrote:
.... snip ... --
Pity the poor electron, floating around minding its own business for
billions of years; and then suddenly Bam!! -- annihilated just so
you could read this posting.


Minor nit - electron-positron annihilation produces gamma rays, not
electricity nor light.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #5
CBFalconer <cb********@yahoo.com> writes:
Walter Roberson wrote:

... snip ...
--
Pity the poor electron, floating around minding its own business for
billions of years; and then suddenly Bam!! -- annihilated just so
you could read this posting.


Minor nit - electron-positron annihilation produces gamma rays, not
electricity nor light.


Minor nit - gamma rays are light. They're just very very very very
very blue.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6
In article <X8*****************@read1.cgocable.net>
philipx <ak***@cogeco.ca> wrote:
IMO, you can freely choose between "#ifdef XYZ" and "#if defined(XYZ)", with
one exception, you cannot test for more than one thing in a single
"#ifdef".
Right.

Also, as a side note, the parentheses are not required when using
the "defined" pseudo-keyword (it is really just a magic identifier,
rather than a keyword, since keywords do not exist in these phases
of translation):

#ifdef X
#if defined(X)
#if defined X

are all synonymous.
consider the following, it's better to use "#if defined" than "#ifdef":

/* Test for the support of C99 Standard */
#if defined(__STDC_VERSION__) && __STD_VERSION__ > 199901L
/* ...something legal in C99 only */


This test is a little odd, because if __STDC_VERSION__ is not
defined, the test:

#if __STDC_VERSION__ > 199901L

is entirely legal and "means" the same thing as:

#if 0 > 199901L

which is of course false. In preprocessor expressions, undefined
identifiers *must* be replaced with 0. This is why:

#if sizeof(int) == 4

*must* produce a diagnostic, because -- assuming you have not done
something silly like "#define sizeof" -- it is syntactically the
same as:

#if 0(0) == 4

Once the required diagnostic has been emitted, a C compiler can go
back and "notice" that "sizeof" is not only not-defined but will
also become a valid keyword later, and then figure out that you
want it to act as though sizeof is allowed in preprocessor expressions.
A suitable diagnostic, for a smart compiler that does this, might
be something annoying like:

foo.c, line 123: warning: I handle "sizeof" the way you mean it
here, because "I M SMRT", but ANSI/ISO C compilers are not
required to, so your code may not work on other systems.

Of course, if you do "#define sizeof" -- e.g.:

#define sizeof(x) 3
#if sizeof(int) == 4
#undef sizeof

-- then other conditions apply.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #7

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

Similar topics

1
by: F. Edward Boas | last post by:
How can you check to see if a type is built-in or typedef'ed? For example, an 8-bit integer could be u_int8_t, uint8_t, or unsigned __int8,...
1
by: Christopher M. Lusardi | last post by:
Hello, Is there anyway to do it other than using : #ifdef VAR1 ... #endif
5
by: lovecreatesbeauty | last post by:
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...
5
by: Hash | last post by:
I have a small doubt, #ifdef _BSD When we use #ifdef preprocessor directive, the parameter which we pass (in this case _BSD) is it defined by...
1
by: baumann | last post by:
hi all, is there any shadow between #ifdef symbol and #if defined(symbol)? thanks in advance. baumann@pan
5
by: None | last post by:
Hello I have an application that will run on WinCE and XP. In order to have the WinCE virtual keyboard pop up when the cursor enters an edit...
1
by: Michael Sgier | last post by:
Hi I get the error: No case-independent string comparison (stricmp, strcasecmp) with the code below. Why...where should stricmp be defined? And...
6
by: anirbid.banerjee | last post by:
Hi, I need to write a macro which would have a lot many conditional #ifdef ... #endif blocks in it. #define _xx_macro (x) { ... \ ... \ /*...
46
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
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...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.