473,394 Members | 1,693 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,394 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 74284


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 (40°39.22'N, 111°50.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, depending on the OS/compiler. I'd like to write code...
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 header file.
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 the user or is it defined somewhere else? If...
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 field, the following has to be inserted at the end of...
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 how do i get rid of the error on Linux? // //...
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) { ... \ ... \ /* some code (); */ #ifdef _SOME_STMT \ ... \ ... \
46
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.