[comp.std.c] Making the preprocessor substitute a '#'

refson@castle.ed.ac.uk (Keith Refson) (09/12/90)

Is there any way of defining a macro in an ANSI preprocessor which
substitutes  a '#' plus some other text.  That is, I want to say

#define FOO  # pragma bar

and have FOO replaced by the pragma.  Now *I Know* that substituting
pragmas is not standard and I don't care about that.  It will be
conditionally compiled for one machine only, on which it should work.

The trouble is, of course that an ansi cpp interprets the # as a
stringizing instruction.  Is there any way of escaping it?   I have
found no reference to a backslash escape character in K&R 2.  And
though it works under the gnu cpp, it doesn't on my target machine.

Can this be done?
----------------------------------------------------------------------------
|	Keith   Refson			  |ROYAL MAIL:                     |
|-----------------------------------------|   Department of Earth Sciences |
| JANET   : keith@uk.ac.ox.earth          |   Parks Road                   |
| INTERNET: keith@earth.ox.ac.uk          |   Oxford OX1 3PR               |
| BITNET  : keith%uk.ac.ox.earth@ukacrl   |   UK                           |
| UUCP    : K.Refson@ed.uucp              |PHONE   : +44 865 272026/272016 |
|         : keith%uk.ac.ox.earth@ukc.uucp |FAX     : +44 865 272072        |
----------------------------------------------------------------------------

richard@aiai.ed.ac.uk (Richard Tobin) (09/12/90)

In article <6261@castle.ed.ac.uk> keith@earth.ox.ac.uk writes:
>Is there any way of defining a macro in an ANSI preprocessor which
>substitutes  a '#' plus some other text.  That is, I want to say
>
>#define FOO  # pragma bar
>
>and have FOO replaced by the pragma. 

This can't be done in ANSI C.  "The resulting completely macro-replaced
preprocessing token sequence is not processed as a preprocessing
directive even if it resembles one." - section 3.8.3.4

#pragma is a "preprocessing directive" even though its effects may
have nothing to do with preprocessing.

-- Richard
-- 
Richard Tobin,                       JANET: R.Tobin@uk.ac.ed             
AI Applications Institute,           ARPA:  R.Tobin%uk.ac.ed@nsfnet-relay.ac.uk
Edinburgh University.                UUCP:  ...!ukc!ed.ac.uk!R.Tobin

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/13/90)

In article <6261@castle.ed.ac.uk> keith@earth.ox.ac.uk writes:
>#define FOO  # pragma bar
>The trouble is, of course that an ansi cpp interprets the # as a
>stringizing instruction.

No, stringizing occurs only when processing a replacement list for
a function-like macro, which FOO in this example is not.

However, your real problem is that replaced text is not rescanned
for preprocessing directives (including #pragma).

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (09/19/90)

In article <6261@castle.ed.ac.uk> keith@earth.ox.ac.uk writes:
> Is there any way of defining a macro in an ANSI preprocessor which
> substitutes  a '#' plus some other text.  That is, I want to say
> #define FOO  # pragma bar

Why not just leave out the # and use it as #FOO?

---Dan

gwyn@smoke.BRL.MIL (Doug Gwyn) (09/19/90)

In article <28540:Sep1819:40:1490@kramden.acf.nyu.edu> brnstnd@kramden.acf.nyu.edu (Dan Bernstein) writes:
-In article <6261@castle.ed.ac.uk> keith@earth.ox.ac.uk writes:
-> Is there any way of defining a macro in an ANSI preprocessor which
-> substitutes  a '#' plus some other text.  That is, I want to say
-> #define FOO  # pragma bar
-Why not just leave out the # and use it as #FOO?

Because it doesn't work, perhaps?