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

C-preprocessor macro question

Hi,

I want to have a CPP macro that tests the value of a token and
returns the string "No" if the token is undefined (or 0) and
returns "Yes" if the token is defined (non-zero).

Then I can have C code that self-diagnoses its configuration with,
e.g.,

#define TKN2YESNO(x) ((x)==0 ? ("No"):("Yes"))
(void)fprintf(stderr,"The token FOO is defined: %s",TKN2YESNO(FOO));
(void)fprintf(stderr,"The token BAR is defined: %s",TKN2YESNO(BAR));

However, my definition of TKN2YESNO() does not work.
The GCC compiler on Linux flags an when I invoke TKN2YESNO() as above:

nco_scm.c:195: error: `FOO' undeclared (first use in this function)

Any help in understanding the cause of this error and how to correct
it to achieve the desired functionality would be appreciated!

Thanks,
Charlie
--
Charlie Zender, su*****@uci.edu, (949) 824-2987, Department of Earth
System Science, University of California, Irvine CA 92697-3100
Visiting NCAR 12/13/03--1/17/04: ***********************************
Voice/FAX: (303) 497-1724/1348, Office: Mesa Lab 259b **************
Nov 14 '05 #1
3 3118

On Tue, 23 Dec 2003, Charlie Zender wrote:

I want to have a CPP macro that tests the value of a token and
returns the string "No" if the token is undefined (or 0) and
returns "Yes" if the token is defined (non-zero).
In general I believe this is impossible, but someone may yet
prove me wrong. To what would your hypothetical macro evaluate in
the following "corner cases"?

#define FOO
#define FOO 0
#define FOO 0+0
#define FOO "bar"
#define FOO +
#define FOO if

Should it yield "Yes", "No", some other defined result, or would
it be allowed to crash completely? Or are you just looking for a
macro to yield "Yes" or "No" based on the integer value of a
#defined or un#defined macro -- in that case, I think you might
be looking for something as simple as

#define P(x) x
#define YESNO(foo) (P(foo)+0)? "Yes": "No")

#define foo 42 yields "Yes"
#define foo 0 yields "No"
#define foo yields "No"

but it may not like complicated expressions.

Then I can have C code that self-diagnoses its configuration with,
e.g.,

#define TKN2YESNO(x) ((x)==0 ? ("No"):("Yes"))
(void)fprintf(stderr,"The token FOO is defined: %s",TKN2YESNO(FOO));
(void)fprintf(stderr,"The token BAR is defined: %s",TKN2YESNO(BAR));

However, my definition of TKN2YESNO() does not work.
The GCC compiler on Linux flags an when I invoke TKN2YESNO() as above:

nco_scm.c:195: error: `FOO' undeclared (first use in this function)


This Won't Work (TM). If the token 'FOO' is *completely* undefined,
it won't macro-expand to anything, and you'll be left with 'FOO',
which the compiler will then treat like any other identifier -- as
if it were a variable name or something. And you'll get errors.
If this is what you want, you'll have to be a *lot* more specific
about what counts as "defined" and what doesn't, and maybe someone
will be nice enough to whip up a hack like the "month-day-year"
preprocessor hack Martin Dickopp did in this thread:
92**************************@posting.google.com
For example, you might get somewhere useful by stringizing the
thing. Other than that, I think you're stuck with

#if defined(foo) && (foo != 0)
...
#endif

which is tedious.

HTH,
-Arthur

Nov 14 '05 #2
Charlie Zender <ze****@uci.edu> wrote:
# Hi,
#
# I want to have a CPP macro that tests the value of a token and
# returns the string "No" if the token is undefined (or 0) and
# returns "Yes" if the token is defined (non-zero).

As a macro processor, CPP is fatally crippled. In particular you cannot
assume it will permit an #if or #ifdef within a #define. I would suggest
using a widely available real macro processor, perhaps m4, or to use a
scripting language like perl or Tcl as if a macro processor.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
Leave it to the Catholics to destroy existence.
Nov 14 '05 #3
Derk Gwen <de******@HotPOP.com> wrote:
Charlie Zender <ze****@uci.edu> wrote:
# I want to have a CPP macro that tests the value of a token and
# returns the string "No" if the token is undefined (or 0) and
# returns "Yes" if the token is defined (non-zero).

As a macro processor, CPP is fatally crippled.
As a C preprocessor, though, the C preprocessor is, if anything,
dangerously overpowered and therefore prone to abuse. As a second-tier
programming language to munge the outcome of your main programming
language it lacks fuck-around-ability power, true, but that's probably
just as well.
In particular you cannot assume it will permit an #if or #ifdef
within a #define.


Actually, you can (indeed, must) assume that it will _not_ permit
complete preprocessing commands within the expansion of a preprocessor
macro.

Richard
Nov 14 '05 #4

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

Similar topics

1
by: Andrew Wied | last post by:
Hello I am writing a macro in Visual Studio .Net 2003, and I was wondering if it is possible for a macro to access properties of objects that are selected. For instance, assume I have the following...
4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
1
by: Ann Baker | last post by:
I had to take a Access 97, 2002 and 2003 test for a temp agency. One of the questions was to "filter the hospital macro to show only the hospitals associated with hospital ID #" Can't remember the...
1
by: Negative Burn | last post by:
I have a form that uses a option group attached to a macro to filter results on the form to display specific workcodes. The form works great. However, when I apply the same function to a subform...
0
by: Alex_H | last post by:
Hello. I am attempting to write a macro that forwards the current item (will be appending text to it) to a specified email address and allow the user to enter in data before it is sent. I found...
3
by: rgrandle | last post by:
I'm trying to figure out how to create a few handy macros for my job at work. I've done a little VBA programming before, but its been a few years since I've touched it, so I could really use some...
3
by: pmorava | last post by:
I have a main table with about 1400 records. These records are distinguised by a department code. I want to print a seperate report for each department code. How do I do this with a macro? Can...
8
by: Peithon | last post by:
Hi, I'm initialising unit test structures using a macro as follows: #define UTMACRO(x) UT##x typedef struct { char * key; char * arr;
3
by: ManicQin | last post by:
Hi all. Is there a way to iterate through a pre-defined list in pre-compile time? (not with for) something like: #define LIST int,double,float,string...... #define MAX 10
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
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...
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)...
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.