473,699 Members | 2,311 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(s tderr,"The token FOO is defined: %s",TKN2YESNO(F OO));
(void)fprintf(s tderr,"The token BAR is defined: %s",TKN2YESNO(B AR));

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 3139

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(s tderr,"The token FOO is defined: %s",TKN2YESNO(F OO));
(void)fprintf(s tderr,"The token BAR is defined: %s",TKN2YESNO(B AR));

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************* *************@p osting.google.c om
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******@HotPO P.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
1208
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 object declared (on a different page, but it can still be instanciated) Class rectangl Private _height as doubl Private _length as doubl Public Property height( Public Property width( End Clas
4
2616
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 before the fwrite. WHY??? Works: fseek(fp, itemloc, SEEK_SET); tmp = ReadLongBE(fp); if (*itemsize == tmp){ printf("\n%X\n",ftell(fp));
1
1608
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 number. Since it was one of those stupid ProveIt tests it would not let you access any of the usual things. All that was on the screen was a tiny form showing 1 of 7 records. when I tried to right click on the Hospital number field it did pop...
1
2918
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 I run into a problem. Once I click on one of the option group buttons Access asks from a parameter for the field that is supposed to be filtered (strWorkCodes, in this case). So far I can't find what I'm doing wrong. I even went as far as to...
0
822
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 this example: Sub CommandButton1_Click() Set myNameSpace = Application.GetNameSpace("MAPI") Set myFolder = myNameSpace.GetDefaultFolder(6)
3
4501
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 help. I'm looking to create a host of macros that will cycle through different formatting options. For example, I want to create a macro, link it to cntrl+shift+F that will modify the fill color the following way: cntrl+shift+F ---> when keyed the...
3
1454
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 anyone help me?
8
366
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
1544
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
8685
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
8613
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9172
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
9032
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
8908
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,...
1
6532
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4374
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
3054
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
3
2008
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.