Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > c preprocessor puzzle

Reply
Thread Tools

c preprocessor puzzle

 
 
marv andersen
Guest
Posts: n/a
 
      11-01-2011
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
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-02-2011
marv andersen <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
marv andersen
Guest
Posts: n/a
 
      11-02-2011
On Nov 2, 12:19*am, Ben Bacarisse <(E-Mail Removed)> wrote:
> marv andersen <(E-Mail Removed)> 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.]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
A puzzle to puzzle you sk A+ Certification 1 07-17-2004 05:19 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
VHDL Preprocessor The Weiss Family VHDL 2 07-14-2004 05:51 AM
Preprocessor conditional compilation variable not being saved Chris P ASP .Net 0 10-28-2003 08:48 PM



Advertisments