473,320 Members | 2,024 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,320 software developers and data experts.

Macro substitution conundrum

I have a C file that contains (among other things) a number of
macros that will be replaced depending on whether or not a compile-time
option is defined. For example, two functions f and g in that file might
be

int f(void)
{
int x ;

x = MY_SYMBOL ;

return x + 3 ;
}

int g(void)
{
int y ;

y = MY_SYMBOL ;

return y + 4 ;
}

We also have, in some include file used by the C file in question,

#ifdef COMPILE_TIME_FLAG
#define MY_SYMBOL 1
#else
#define MY_SYMBOL 0
#endif

What I would like is for x to be assigned 1 or 0, depending on whether or
not COMPILE_TIME_FLAG is defined, while y is to be assigned 0 no matter
whether or not COMPILE_TIME_FLAG is defined. As you can see, this is a
toy example that is trivially solved by other means; I am just
illustrating a general question.

Something I tried was

#define NO_REPLACE

int g(void)
{
int y ;

#ifdef COMPILE_TIME_FLAG
#ifdef NO_REPLACE
#undef COMPILE_TIME_FLAG
#define SET_COMPILE_TIME_FLAG
#endif
y = MY_SYMBOL ;
#ifdef SET_COMPILE_TIME_FLAG
#define COMPILE_TIME_FLAG
#endif
#endif

return y + 4 ;
}

but it does not work (and it is ugly to boot.)

Any ideas as to how to do this? Actually, is it doable?

Dec 13 '07 #1
5 1206
Harold Weissman <Ha*******@hotmail.comwrote in comp.lang.c:
What I would like is for x to be assigned 1 or 0, depending on whether
or not COMPILE_TIME_FLAG is defined, while y is to be assigned 0 no
matter whether or not COMPILE_TIME_FLAG is defined.

y = 0;
I'm gonna assume that I misunderstand the problem.

--
Tomás Ó hÉilidhe
Dec 13 '07 #2
Harold Weissman wrote:
I have a C file that contains (among other things) a number of
macros that will be replaced depending on whether or not a compile-time
option is defined.
[Snip]
What I would like is for x to be assigned 1 or 0, depending on whether or
not COMPILE_TIME_FLAG is defined, while y is to be assigned 0 no matter
whether or not COMPILE_TIME_FLAG is defined.
If you've got two different types of behaviour, you need two symbols,
IMHO.

However, like Tomas, I don't believe I understand what you're getting
at with this question.

Rather than giving us the trivial version, why don't you describe what
you are trying to _achieve_ and let us make some suggestions based on
that?
Dec 13 '07 #3
Harold wrote:
) I have a C file that contains (among other things) a number of
) macros that will be replaced depending on whether or not a compile-time
) option is defined. ...
)
) What I would like is for x to be assigned 1 or 0, depending on whether or
) not COMPILE_TIME_FLAG is defined, while y is to be assigned 0 no matter
) whether or not COMPILE_TIME_FLAG is defined. As you can see, this is a
) toy example that is trivially solved by other means; I am just
) illustrating a general question.

You can #undefine a macro and then #define it again with a different value.
Maybe that helps ?
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Dec 13 '07 #4
On Thu, 13 Dec 2007 19:05:26 +0000, Mark Bluemel wrote:
Rather than giving us the trivial version, why don't you describe what
you are trying to _achieve_ and let us make some suggestions based on
that?
My aim is to understand what it is that can and can't be done
with the preprocessor. In this sense, one thing that would be useful is
for somebody to explain why the approach I proposed does not work.

Dec 13 '07 #5
Harold Weissman <Harold...@hotmail.comwrote:
Mark Bluemel wrote:
Rather than giving us the trivial version, why don't
you describe what you are trying to _achieve_ and let
us make some suggestions based on that?

My aim is to understand what it is that can and can't be
done with the preprocessor.
See Boosts preprocessor library.

Bare in mind, despite being compatible, you won't see much
usage of it in many C programs.

--
Peter
Dec 13 '07 #6

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

Similar topics

6
by: Raghuveer Pallikonda | last post by:
Hi, I am trying to stub out debug log messages in my application, if the logging subsystem is not enabled.. For e.g a invocation #define LOGMSG !Logger::Enabled() ? false : Logger::LogMsg so...
8
by: Siemel Naran | last post by:
#define EXPECT_ASSERT(x) { if (!x) expect_assert(localVariable, __FILE__, __LINE__, #x); } MSVC7 gives an error: "error C2014: preprocessor command must start as first nonwhite space".
1
by: me | last post by:
Hi guys I want to insert a load of pieces of data into a map The map has an std::string representing a field name as the key, and the value is a struct with 2 members - the field length and a...
7
by: Newbie_sw2003 | last post by:
Where should I use them? I am giving you my understandings. Please correct me if I am wrong: MACRO: e.g.:#define ref-name 99 The code is substituted by the MACRO ref-name. So no overhead....
14
by: Malcolm | last post by:
Hi, I have the following which fails with "disagreement in number of macro arguments" when compiling with Imagecraft ICCAVR. Has anyone got any ideas - its not vital but would make the code a...
1
by: Rodolfo | last post by:
Hello, there's another languages that can do a macro substitution, how can I do this in Csharp. This is an example of what I want to do Dataset ds = new Dataset; string a = "ds"; DataSet...
4
by: Don | last post by:
I think "macro substitution" is the correct term for what I want to do, but, to be sure, here is a description of what I'd like to know is possible: I want to be able to create a create an object...
3
by: XHengDF | last post by:
recent days. i am confuse on the macros in boost. could anyone tell me the replacement rules of the macros in c++, or somelinks about the macros in boost thanks
37
by: junky_fellow | last post by:
hi guys, Can you please suggest that in what cases should a macro be preferred over inline function and viceversa ? Is there any case where using a macro will be more efficient as compared to...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.