c preprocessor puzzle

Discussion in 'C Programming' started by marv andersen, Nov 1, 2011.

  1. Hi,

    What should the following produce after preprocessing
    #define y(x) x
    #define x(a) y(a
    x(1) x(2)))

    I think we should get '1 2', but cpp on linux seems to think this
    expands to an invalid use of y? My reasoning is that we should get
    the following:
    y(1 x(2)))
    y(1 y(2))
    y(1 2)
    1 2

    .... but maybe I'm missing something.

    Any help much appreciated!

    Marv
    marv andersen, Nov 1, 2011
    #1
    1. Advertising

  2. marv andersen <> writes:

    > What should the following produce after preprocessing
    > #define y(x) x
    > #define x(a) y(a
    > x(1) x(2)))
    >
    > I think we should get '1 2', but cpp on linux seems to think this
    > expands to an invalid use of y? My reasoning is that we should get
    > the following:
    > y(1 x(2)))
    > y(1 y(2))
    > y(1 2)
    > 1 2
    >
    > ... but maybe I'm missing something.


    cpp is correct. The key wording from the standard is this (from
    6.10.3.1 para 1):

    "Before being substituted, each argument’s preprocessing tokens are
    completely macro replaced as if they formed the rest of the
    preprocessing file; no other preprocessing tokens are available."

    Is that enough? Because this looks like coursework, I am inclined to
    leave it at that, but if you are still puzzled, say so, and I'll include
    a fuller explanation.

    --
    Ben.
    Ben Bacarisse, Nov 2, 2011
    #2
    1. Advertising

  3. On Nov 2, 12:19 am, Ben Bacarisse <> wrote:
    > marv andersen <> writes:
    > > What should the following produce after preprocessing
    > > #define y(x) x
    > > #define x(a) y(a
    > > x(1) x(2)))

    >
    > > I think we should get '1 2', but cpp on linux seems to think this
    > > expands to an invalid use of y?  My reasoning is that we should get
    > > the following:
    > > y(1 x(2)))
    > > y(1 y(2))
    > > y(1 2)
    > > 1 2

    >
    > > ... but maybe I'm missing something.

    >
    > cpp is correct.  The key wording from the standard is this (from
    > 6.10.3.1 para 1):
    >
    >   "Before being substituted, each argument’s preprocessing tokens are
    >   completely macro replaced as if they formed the rest of the
    >   preprocessing file; no other preprocessing tokens are available."
    >
    > Is that enough?  Because this looks like coursework, I am inclined to
    > leave it at that, but if you are still puzzled, say so, and I'll include
    > a fuller explanation.
    >
    > --
    > Ben.


    Thanks - I see my mistake, the extra )s aren't visible when the
    argument '1 x(2)' is being expanded, as we're 'argument expanding',
    not 'rescanning' at this point, so we get as far as '1 y(2' but then
    find we're missing the closing ). [Not coursework BTW, just trying to
    understand cpp a bit better.]
    marv andersen, Nov 2, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    3
    Views:
    6,635
    Nedu N
    Aug 6, 2003
  2. dwa

    Design Puzzle!

    dwa, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    355
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jun 10, 2004
  3. Shankara Narayanan

    Booking puzzle....

    Shankara Narayanan, Jun 17, 2004, in forum: ASP .Net
    Replies:
    20
    Views:
    908
    bredal Jensen
    Jun 30, 2004
  4. VB Programmer
    Replies:
    2
    Views:
    423
    Alan Lambert
    Nov 4, 2004
  5. Cronus
    Replies:
    1
    Views:
    643
    Paul Mensonides
    Jul 15, 2004
Loading...

Share This Page