473,895 Members | 2,293 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 27943
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
2348
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 multible_defined_var actually gets defined. file2.c will get the external definition only because...
5
2416
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 new( size_t size, MemoryHandler* memHandler, AllocationType
15
2189
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
8433
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
2685
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
2160
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
3230
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
26467
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
2612
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 accept void*. There are such functions,
0
9990
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11249
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
10847
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
10937
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
9651
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5865
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
4693
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
2
4288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3298
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.