[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::decc

Title:DECC
Notice:General DEC C discussions
Moderator:TLE::D_SMITHNTE
Created:Fri Nov 13 1992
Last Modified:Fri Jun 06 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:2212
Total number of notes:11045

2129.0. "Macro expansion and quotation marks" by IOSG::MARSHALL () Thu Mar 27 1997 09:49

I have a macro, FOO say, from a header file.  I need the value of FOO in a C
string, eg if FOO has the value 123, I need the sequence "123".  I need to
generate this string at compile time, as I can't make any assumptions about the
value of FOO (before anyone suggests I hard-code the required string! :-)

I've tried playing around with # and ## in macro expansion, and the best I can
come up with is:

#define FOO 123

#define quote2(y) #y
#define quote(x) quote2(x)

...

char *s = quote(FOO);


which works but is a bit messy and convoluted.  Is there an easier way?  You
can't call quote2 directly with FOO, because the # takes precedence over the
expansion of FOO, and I get "FOO".  I tried using ## to split things up, and
hence delay stringization until FOO had been expanded, but that doesn't work
either.

Any other ideas?

Thanks,
Scott
T.RTitleUserPersonal
Name
DateLines
2129.1SPECXN::DERAMODan D'EramoThu Mar 27 1997 10:497
        Convoluted or not, that is the "standard" way to do it.  I
        can't remember any similar how-to notes that suggested
        anything else.
        
        Convoluted is in the eye of the beholder. :-)
        
        Dan
2129.2that 's itDECC::PARKSThu Mar 27 1997 13:523
Dan's right.  That's the way to do it.

\John (cpp author)
2129.3"Standard" way to do itDECCXL::RMEYERSRandy MeyersThu Mar 27 1997 14:494
In fact, .0 closely matches the example in the ANSI C Standard showing
how this stuff works.

Think of the technique as just another C idiom to be learned.
2129.4How do I do this with /STANDARD=VAXC?SMAUG::GARRODIBM Interconnect EngineeringThu Mar 27 1997 18:0520
    Interesting. I came here to ask about the same question. But I need
    this to work with
    
    /STANDARD=VAXC
    
    so my question is:
    
    How do I get the vakue of FOO (which is 123) to be expanded to "123"
    when using /STANDARD=VAXC.
    
    I beliebe the reason the technique in .0 doesn't work with
    /STANDARD=VAXC is because of the Incompatible Common C extension
    documented on page D2 of the DECC manual.
    
    "During macro replacement, an argument's preprocessing tokens are NOT
     macro replaced before the macro is expanded".
    
    So I guess my question is how can I get them macro expanded?
    
    Dave
2129.5SPECXN::DERAMODan D'EramoThu Mar 27 1997 20:3318
        In /STANDARD=COMMON mode, the following
        
        	#define Q2(s) "s"
        	#define Q(x) Q2(x)
        
        	#define FOO 123
        
        	static const char foo[] = Q(FOO);
        
        yields
        
        	static const char foo[] = "123";
        
        but in /STANDARD=VAXC mode it yields
        
        	static const char foo[] = "FOO";
        
        Dan
2129.6What's the reason for COMMON and VAXC to be different here?EDSCLU::GARRODIBM Interconnect EngineeringFri Mar 28 1997 09:4515
    Re .5
    
    Yes this is axactly the issue I'm asking about. What is it that defines
    the behaviour of /STANDARD=COMMON to be different from /STANDARD=VAXC
    in this regard. The documentation implies that COMMON is a subset of VAXC.
    
    I've now tried every way I can think of to get quoting and substitution
    to happen at the same time. I can't find any way that seems to work
    /STANDARD=VAXC. Unfortunately I need to use /STANDARD=VAXC because
    I don't have the time available right now to make all the countless
    lines of code ANSI compliant.
    
    Thanks,
    
    Dave
2129.7can't do it in -vaxc modeDECC::PARKSFri Mar 28 1997 16:5722
Re .4

I do not know of any way to stringize the expansion of a macro
in VAXC mode.  And your reasoning is correct -- it's because in
VAXC mode, macro arguments are never expanded before substitution.

I tried vaxc (the real compiler) and could find no way to do it
with that compiler either.

Re .6

Sorry for the confusion.  COMMON is not a subset of VAXC though 
they are close.  COMMON is intended to match the behavior of the
pcc compiler on UNIX systems.  VAXC is intended to match the 
behavior of the vaxc compiler.

I can think of at least one other difference in the macro processor:
VAXC mode allows for recursive macro expansion, COMMON doesn't.

Have you tried COMMON mode?  That may be your best bet.

\John