[comp.lang.c] Second question on #define

wayne@ames.arpa (11/02/87)

Having gotten tremendous response to my last question about #define, I
now have another one.  I have a large number of manifest constants of
the form
                    GREATBIGLONGPREFIX_SHORTTHING

I would like to have a macro accept only SHORTTHING as an argument but
use the full constant name in the generated code.  I (naively) tried

        #define   WIDGET(x)   printf("%d\n", GREATBIGLONGPREFIX_x)

but of course this didn't work (didn't substitute x).  I guess this is
related to a discussion I saw a while back on getting xy out of two
separate parameters x and y (that is, concatenation), but I don't
remember the results of that discussion well enough to apply it here.

Any assistance would as always be muchly appreciated ...   Thnx.

      Wayne Hathaway                  ultra!wayne@Ames.ARPA
      Ultra Network Technologies
      2140 Bering drive               with a domain server:
      San Jose, CA 95131                 wayne@Ultra.COM
      408-922-0100

john@leo.UUCP ( John McCurry) (11/04/87)

In article <10104@brl-adm.ARPA>, ultra!wayne@ames.arpa (Wayne Hathaway) writes:
>   I have a large number of manifest constants of the form
>                     GREATBIGLONGPREFIX_SHORTTHING
> 
> I would like to have a macro accept only SHORTTHING as an argument but
> use the full constant name in the generated code.  I (naively) tried
>         #define   WIDGET(x)   printf("%d\n", GREATBIGLONGPREFIX_x)

Our compiler uses the "Reiser" (spelling?) version of cpp.  This version
uses /**/ for concatinating things - as in:

	#define  WIDGET(x)   printf("%d\n", GREATBIGLONGPREFIX_/**/x);
or
	#define  WIDGET(x)   printf("%d\n", x/**/_GREATBIGLONGSUFFIX);

I don't have access to an ANSI standard compiler but our local expert
says that
	PREFIX_##x  will (should) work.

I am not, by the way, a compiler guru - I just use the beasts.  Any
mistakes in the above posting are probably accidental.
-- 
<insert random witticism here>
John J. McCurry 
Computer Consoles Inc. CPD, Irvine, Ca.		uunet!ccicpg!leo!john

rbutterworth@orchid.UUCP (11/05/87)

In article <10104@brl-adm.ARPA>, ultra!wayne@ames.arpa (Wayne Hathaway) writes:
> I have a large number of manifest constants of
> the form
>                     GREATBIGLONGPREFIX_SHORTTHING
> I would like to have a macro accept only SHORTTHING as an argument but
> use the full constant name in the generated code.  I (naively) tried
>         #define   WIDGET(x)   printf("%d\n", GREATBIGLONGPREFIX_x)
> but of course this didn't work (didn't substitute x).

Put the following into your common header file:
    #if defined(__STDC__)
    #   define JOIN(x)x##
    #else
    #   define JOIN(x)x
    #endif

Then, you can do things like
    #define WIDGET(x) printf("%d\n", JOIN(GREATBIGLONGPREFIX_)x)

This works on all the cpp implementations I've used.

If you find one that it doesn't work for, you only need to add
another condition for the definition of JOIN in the header
file to make it work the way that that cpp wants it, and you
don't have to change any of your source code.