473,563 Members | 2,897 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

#ifdef within a #define

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 \
... \
... \
#endif \
... \
... \
}

Is such a thing possible, or I need to make the #ifdefs out side the
#define, and for each #ifdef .. #else ... #endif, I should write a
different #define? I am using XLC compiler. Is this compiler
dependent?

Thanks
Anirbid

Feb 17 '07 #1
6 27784
an************* *@gmail.com wrote:
Hi,

I need to write a macro which would have a lot many conditional
#ifdef ... #endif blocks in it.
You are out of luck. Even if a macro expansion produces
something that resembles a preprocessor directive, it is not
a preprocessor directive.
#define _xx_macro (x) {
... \
... \ /* some code (); */
#ifdef _SOME_STMT \
... \
... \
#endif \
... \
... \
}

Is such a thing possible, or I need to make the #ifdefs out side the
#define, and for each #ifdef .. #else ... #endif, I should write a
different #define? I am using XLC compiler. Is this compiler
dependent?
You need to "turn it inside out:" write several different
definitions of the macro, and use #ifdef (and #ifndef and #if)
to choose which definition is used:

#ifdef SOME_STMT
#define xx_macro ... SOME code ...
#elif defined THAT_STMT
#define xx_macro ... THAT code ...
#else
#define xx_macro ... ELSE code ...
#endif

Note that the tests are made at the point where they appear in
the source code. The xx_macro definition will not be altered
by subsequent changes to SOME_STMT and THAT_STMT; it will still
expand according to the way it was originally defined. (Unless
you #undef and re-#define it, of course.)

As an aside, avoid using identifiers that start with _.
Some of them are available for your use, but many are reserved
either for the language or for the implementation, so your uses
of _xx_macro and _SOME_STMT run the risk of clashing with other
definitions that you cannot control.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 17 '07 #2
On Feb 17, 5:46 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
anirbid.baner.. .@gmail.com wrote:
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 \
... \
... \
#endif \
... \
... \
}
Is such a thing possible, or I need to make the #ifdefs out side the
#define, and for each #ifdef .. #else ... #endif, I should write a
different #define? I am using XLC compiler. Is this compiler
dependent?
The other possibility would be to turn this into a real function:

#define _xx_macro (x) real_function(x );

void real_function(i nt x) /* or whatever type */
{
...
... /* some code (); */
#ifdef _SOME_STMT
...
...
#endif
...
...
}

The benefits of using actual functions instead of macros can be
enormous in terms of code clarity and maintainability , so unless you
absolutely need it to be a macro, I'd suggest something like this.

Michael

Feb 17 '07 #3
Michael wrote:
On Feb 17, 5:46 am, Eric Sosman <esos...@acm-dot-org.invalidwrot e:
>anirbid.baner. ..@gmail.com wrote:
>>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 \
... \
... \
#endif \
... \
... \
}
Is such a thing possible, or I need to make the #ifdefs out side the
#define, and for each #ifdef .. #else ... #endif, I should write a
different #define? I am using XLC compiler. Is this compiler
dependent?

The other possibility would be to turn this into a real function:
[...]
Note that the O.P.'s example is not a function-like macro.

--
Eric Sosman
es*****@acm-dot-org.invalid
Feb 18 '07 #4
>an************ **@gmail.com wrote:
>I need to write a macro which would have a lot many conditional
#ifdef ... #endif blocks in it.
In article <s_************ *************** ***@comcast.com >
Eric Sosman <es*****@acm-dot-org.invalidwrot e:
You are out of luck. Even if a macro expansion produces
something that resembles a preprocessor directive, it is not
a preprocessor directive.
Indeed, this is one of the few clear-and-obvious statements in the
C standards. I might even bet that C1x will deliberately screw this
up, just to make things confusing. :-)
>#define _xx_macro (x) {
... \
... \ /* some code (); */
#ifdef _SOME_STMT \
... \
... \
#endif \
... \
... \
}
You need to "turn it inside out:" write several different
definitions of the macro, and use #ifdef (and #ifndef and #if)
to choose which definition is used:

#ifdef SOME_STMT
#define xx_macro ... SOME code ...
#elif defined THAT_STMT
#define xx_macro ... THAT code ...
#else
#define xx_macro ... ELSE code ...
#endif
There is another alternative: give just the "#ifdef"ed sections
their own names. For instance, suppose you want BIG_MACRO to
expand as if it were:

do {
some code here;
#ifdef FOO
code to do foo;
#endif
more code here;
#ifdef BAR
code to do bar;
#else
code to do non-bar;
#endif
final code here;
} while (0)

Now:

#define BIG_MACRO \
do { \
some code here; \
#ifdef FOO \
code to do foo; \
#endif \
...

is no good, because -- as Eric Sosman noted -- even if you manage
to put a preprocessor directive into a "#define", it will not be
examined again later. But suppose, instead, we define two auxiliary
"little macros":

#ifdef FOO
# define LITTLE_FOO_MACR O code to do foo
#else
# define LITTLE_FOO_MACR O /* nothing */
#endif

#ifdef BAR
# define LITTLE_BAR_MACR O code to do bar
#else
# define LITTLE_BAR_MACR O code to do non-bar
#endif

#define BIG_MACRO \
do { \
some code here; \
LITTLE_FOO_MACR O; \
more code here; \
LITTLE_BAR_MACR O; \
final code here; \
} while (0)

The "big" macro now makes use of a series of "little" macros.
These "little macros" must still be defined at the point of
use, which is different from what Eric describes:
>Note that the tests are made at the point where they appear in
the source code. The xx_macro definition will not be altered
by subsequent changes to SOME_STMT and THAT_STMT; it will still
expand according to the way it was originally defined. (Unless
you #undef and re-#define it, of course.)
Here "BIG_MACRO" literally contains "LITTLE_FOO_MAC RO". It is at
the point you *use* the big macro, when LITTLE_FOO_MACR O comes out
of the preprocessor, that the preprocessor notices that LITTLE_FOO_MACR O
is also a "#define", and expands it. So you are free to change
the small macros around, after which you will get a different
expansion from the big macro.
As an aside, avoid using identifiers that start with _.
Some of them are available for your use, but many are reserved
either for the language or for the implementation, so your uses
of _xx_macro and _SOME_STMT run the risk of clashing with other
definitions that you cannot control.
Yes -- unless you are "the implementor". It is easy to tell whether
you are "the implementor": you are this person if and only if you
are writing the C compiler itself.
--
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.
Feb 18 '07 #5
Chris Torek <no****@torek.n etwrites:
[...]
> As an aside, avoid using identifiers that start with _.
Some of them are available for your use, but many are reserved
either for the language or for the implementation, so your uses
of _xx_macro and _SOME_STMT run the risk of clashing with other
definitions that you cannot control.

Yes -- unless you are "the implementor". It is easy to tell whether
you are "the implementor": you are this person if and only if you
are writing the C compiler itself.
*Or* the standard C library (i.e., the library code that implements
the standard headers defined in the standard).

--
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.
Feb 18 '07 #6
# >>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 \
# >> ... \
# >> ... \
# >> #endif \
# >> ... \
# >> ... \
# >>}
# >>Is such a thing possible, or I need to make the #ifdefs out side the
# >>#define, and for each #ifdef .. #else ... #endif, I should write a
# >>different #define? I am using XLC compiler. Is this compiler
# >>dependent?

You can sometimes simulate with an include file.

So you have something
macro.define:
#ifndef x
#error parameter 'x' is not defined.
#endif
/* some code (); */
#ifdef _SOME_STMT
...
...
#endif
...
...
#undef x
somecode.c
...
#define x ABC
#include "macro.defi ne"
...
#define x PQR
#include "macro.defi ne"
...
#define x (X+Y+Z)
#include "macro.defi ne"
...

Or else use a real macro processor.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Quit killing people. That's high profile.
Feb 19 '07 #7

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

Similar topics

1
2332
by: Christopher M. Lusardi | last post by:
Hello, How is this possible. If I have two separate files that include the same dot h file as described below why am I allowed to access the same value for a constant. I compiled this program using a makefile. (Sorry, I can't post the long makefile or program.) What the code is suppose to do is: main () includes the file and the variable...
5
2375
by: kUfa.scoopex | last post by:
Hi there! I have a small problem, and i really dont see any convenient way to fix it. Basically, i'd like to overload global new operators, into something like this: void *operator new( size_t size ); void *operator new( size_t size, AllocationType allocType ); void *operator new( size_t size, MemoryHandler* memHandler ); void *operator...
15
2154
by: Jorge Naxus | last post by:
Hello I would like to write a macro like that: #ifdef DEBUG #define printj(...) printf(...) #else #printj(...) #endif
2
8380
by: Whywhat | last post by:
Hi! I have a name conflict between my class and windows.h header. The problem is because of windows.h contains GetMessage macro and my class a method with the same name. Thus the macro replaces my method declaration in the main module which contains these lines: #include <windows.h> #include "myclass.h"
7
2668
by: Kenneth Brody | last post by:
Am I correct that using __FILE__ and __LINE__ within a macro will expand to the filename and line in which the macro is invoked, rather than where it is defined? For example, in a header file: #ifdef DEBUG #define LOGIT(note) DoLogging(__FILE__,__LINE__,note) #else #define LOGIT(note)
7
2152
by: Nobody | last post by:
Anyone have a clean way of solving this define issue? In Windows, there are sometimes unicode functions and multibyte functions... the naming convention used is FunctionA for multibyte and FunctionW for unicode... So basically what happens is: void FunctionA(); void FunctionW();
1
3205
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
26401
by: canoewhiteh2o | last post by:
I am converting a couple of C header files to C#. It is mainly just a bunch C structs but I am not sure how to handle the #ifdef and #ifndef in C#. For example: #ifndef DATE_TIME #define DATE_TIME unsigned long #endif // NOT DATE_TIME The #define DATE_TIME I am handling with: public const UInt32 DATE_TIME;
10
2580
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory to be used as a Type structure. The background: I was beaten many times (it surely is not my exclusive experience) by functions which return and...
0
7665
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
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
7888
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. ...
0
8106
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...
0
7950
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...
1
5484
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...
0
5213
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
3643
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...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.