473,657 Members | 2,753 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#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 74340


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.co m>,
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.c om, 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********@yah oo.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_Keit h) 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.cgo cable.net>
philipx <ak***@cogeco.c a> 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
10743
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 like: #if defined(u_int8_t) typedef u_int8_t BYTE; #elif defined (uint8_t) typedef uint8_t BYTE; #elif defined (__int8) typedef unsigned __int8 BYTE
1
2431
by: Christopher M. Lusardi | last post by:
Hello, Is there anyway to do it other than using : #ifdef VAR1 ... #endif
5
5773
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
3901
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 the definition is somewhere where can i find the defnitions ?
1
1553
by: baumann | last post by:
hi all, is there any shadow between #ifdef symbol and #if defined(symbol)? thanks in advance. baumann@pan
5
2362
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 each dialog after all the edit fields have been created: CONTROL "",IDC_STATIC,"SIPPREF",NOT WS_VISIBLE,-10,-10,5,5
1
3213
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? // // Function portability // #ifndef HAVE_STRICMP
6
27835
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
17217
by: mattia | last post by:
I've see in some code: #ifdef __cplusplus extern "C" { #endif what does it mean? Thanks
0
8297
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
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

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.