473,396 Members | 1,779 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,396 software developers and data experts.

conditional expansion of a macro

Hi,
Can I use some conditional operator to select a part of code to be expanded.
What I meant was... Can I do something like this..

#define func(arg1) \
if arg1 == abc\ \* this line should not expand but just test*\
some code\
else\ \* this line should not expand but just test*\
some other code


Thanks
Oct 1 '08 #1
3 6805
donbock
2,426 Expert 2GB
The macro will be replaced by its definition (after suitable argument replacement), so consider whether the expanded code is what you want to see there.

A common concern with conditional code is what happens if it is invoked from within another conditional block. For example:
Expand|Select|Wrap|Line Numbers
  1. #define demo(arg) if (arg > 0) blink()
  2. if (this < that)
  3.    demo(that);
  4. else
  5.    demo(this);
  6. ... expands to ...
  7. if (this < that)
  8.    if (that > 0) blink();
  9. else
  10.    if (this > 0) blink();
Notice that the 'else' ends up being associated with if(that>0) instead of if(this<that) as you would have expected from reading the source code.

The typical idiom used to prevent this from happening is to encapsulate any multi-line macro definition as follows:
Expand|Select|Wrap|Line Numbers
  1. #define demo(arg) \
  2. do { \
  3. if (arg > 0) blink(); \
  4. } while(0)
Notice that all of the source lines of the macro definition are enclosed within a set of braces, making them act like a single instruction. This prevents the else confusion described above; it also prevents you from falling out early if the macro is invoked within a non-bracketed if statement.
Notice that there is no semi-colon after while(0). This forces the programmer to terminate the macro invocation with a semi-colon.
Oct 2 '08 #2
Yeah Donbock what you said was certainly correct in it's own context but I guess I was not clear enough in my question.
Basically what I am asking is, why this peice of code dosen't work and what is the possible solution.

#ifdef ABC
#define TEST /
cout<<"abc"
#else
#define TEST
cout<<"xyz"
#endif

int main()
{
TEST;
#define ABC
TEST
#undef ABC
TEST
return 0;
}
=============
Output:xyzxyzyxz

Thanks
Oct 3 '08 #3
JosAH
11,448 Expert 8TB
That's the way cpp (the C Macro PreProcessor) works: (re)defining ABC after
macro TEST has been defined doesn't (re)define macro TEST. A macro processor
simply runs through text, from top to bottom, and it does what it has to do.

kind regards,

Jos
Oct 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Caleb Hattingh | last post by:
Hi Here is a script I want to be able to write (explanation appears after): *** start of script *** import MyCustomMacroLib # This does the magic I would like help for. # This is not...
16
by: John Smith | last post by:
Lets say you have a function "debug printf" which works like printf but will get left out if _DEBUG flag is not defined along with the strings which are to be printed. Now I defined this: ...
1
by: ammarton | last post by:
Hello all...I'm a bit new to working with Macros in Access so forgive me if the terminology I use is not accurate. To preface this, basically I am using a form on a replicated database so the...
3
by: Ark | last post by:
Hello, NG, Please, help on this snippet: #define CAT(a,b) a##b #define COMMENT CAT(/,/) COMMENT This is a comment Should it compile? It passes MS C/C++ 13.0 (Visual Studio 2002) and fails...
7
by: reppisch | last post by:
Hi Ng, i am looking for a method of expanding a macro while the rest of the code remains untouched. I have some code which does macro voodo / ifdef's which i would like to strip and simplify. ...
4
by: ImOk | last post by:
I come from the Visual Foxpro world, which is one reason I love PHP. VFP is a scripting type language with macro substitution abilities similar to PHP. Besides the regular expansion I can do...
2
by: srinu.fsl | last post by:
there's a MACRO call : MACRO1(cnf) and its expansion is : #define MACRO1(cnf) (((cnf) != TRUE)? (CLEANUP(FAIL)):(err = SUCCESS)); #define CLEANUP(a)
3
by: casul | last post by:
Hi All, I was told there were a few macro gurus on this group :) I'm trying to define a macro that will allow me to write the following code : #include MY_MACRO( NAME, SPACE )
5
by: Srinivas Mudireddy | last post by:
Hi, We have bunch of message levels and depending on whether that level is turned on, messages of that level are printed. For example, we have levels like MSG_LOW, MSG_MED etc. We want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
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,...

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.