Article View: gnu.gcc.help
Article #51760Re: gnu-cpp concatenation
From: Joe Buck
Date: Mon, 12 Apr 1993 19:11
Date: Mon, 12 Apr 1993 19:11
61 lines
2481 bytes
2481 bytes
In article <1qcc03$gl3@genesis.ait.psu.edu> sba@astro.psu.edu (Stuart Anderson) writes: >How does one concatenate strings with the gnu-cpp? Your question should read "How does one concatenate strings with any ANSI C compiler?" > >Example, I would like the output of the following macro to read >"calling the fortran subroutine time_" > *** NOT *** >"calling the fortran subroutine func" >(note: the Sun cpp does this OK) The unbundled Sun cpp is a so called "Reiser" C preprocessor (e.g. broken according to the ANSI C standard). Here's what you need to know about ANSI C: First, two adjacent strings are always concatenated: e.g. "hello " "world" is the same as "hello world". Second, the preprocessor does not expand macro arguments inside quotes: #define foo(arg) "arg" returns "arg" no matter what the argument is. There are two operators that may be used in macro definitions: the stringize operator # and the token concatenation operator ##. The following Reiser preprocessor trick #define PASTE(x,y) x/**/y is illegal according to ANSI -- instead you write #define PASTE(x,y) x ## y Back to your request: you want to paste an underscore to the end of your argument, and you want to end up with a single string. To paste on the underscore you could say. #define FORTRAN(func) func ## _ Now, you cannot say "Calling the fortran subroutine FORTRAN(time)" since macros are not expanded inside quotes. Instead you could say #define STR(func) #func #define CALLING_FORTRAN(func) "Calling the fortran subroutine " STR(func) "_" If you view the C preprocessor output this will look like three strings, but it can be used as a single string. These rules are obeyed by all ANSI C preprocessors. Unfortunately, K&R C preprocessors are quite different. -- Joe Buck jbuck@ohm.berkeley.edu
Technical Details
Message-ID:
<1qcetg$qv6@agate.berkeley.edu>Path:
arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!agate!forney.berkeley.edu!jbuckReferences:
<1qcc03$gl3@genesis.ait.psu.edu>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,6e490088e2db249a X-Google-Attributes: gid11318c,public X-Google-ArrivalTime: 1993-04-12 12:15:59 PST Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!agate!forney.berkeley.edu!jbuck From: jbuck@forney.berkeley.edu (Joe Buck) Newsgroups: gnu.gcc.help Subject: Re: gnu-cpp concatenation Date: 12 Apr 1993 19:11:44 GMT Organization: U. C. Berkeley Lines: 61 Distribution: world Message-ID: <1qcetg$qv6@agate.berkeley.edu> References: <1qcc03$gl3@genesis.ait.psu.edu> NNTP-Posting-Host: forney.berkeley.edu Cc: sba@astro.psu.edu