Error in Preprocessing

N

Naren

Hello,

Could anyone explain why I get an error when I compile this in MSVC6.0
compiler

#include <stdio.h>

#define PP 1,2,3,4

#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )

#define PP_Val PP_Func(PP)

int main()

{

printf ("\n%d", PP_Val );

return 0;

}

Thanks in advance,

Regards,

Naren.
 
P

pete

Naren said:
Hello,

Could anyone explain why I get an error when I compile this in MSVC6.0
compiler

#include <stdio.h>

#define PP 1,2,3,4

That's the same as

#define PP (1,2,3,4)

#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )

#define PP_Val PP_Func(PP)

#define PP_Val PP_Func(PP, PP, PP, PP)
 
D

Dan Pop

In said:
Could anyone explain why I get an error when I compile this in MSVC6.0
compiler

#include <stdio.h>

#define PP 1,2,3,4

#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )

#define PP_Val PP_Func(PP)
^^^^^^^^^^^^^^^^^^^^^^^^^^
Replace this line by:

#define XPP_Func(arg) PP_Func(arg)
#define PP_Val XPP_Func(PP)

Otherwise, PP_Func will be invoked with PP as argument, instead of its
expansion.
int main()

{

printf ("\n%d", PP_Val );

return 0;

}

When the argument of a macro is another macro, you need an extra level
of macro expansion, so that the target macro is invoked with the
macro argument expanded. This is also explained in the FAQ.

After making the change suggested above and removing the include line
(it is necessary for the program correctness, but not if the program is
only preprocessed, as it will generate tons of preprocessor output),
the preprocessor generates the following output:

fangorn:~/tmp 223> cat test.c
#define PP 1,2,3,4
#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )
#define XPP_Func(arg) PP_Func(arg)
#define PP_Val XPP_Func(PP)

int main()
{
printf ("\n%d", PP_Val );
return 0;
}
fangorn:~/tmp 224> gcc -ansi -E test.c
# 1 "test.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "test.c"





int main()
{
printf ("\n%d", ( ((1)*1) + ((2) * 2) +((3) * 3) + ((4) * 4) ) );
return 0;
}

Dan
 
D

Dan Pop

In said:
That's the same as

#define PP (1,2,3,4)
Nonsense.


#define PP_Val PP_Func(PP, PP, PP, PP)

More nonsense.

With your suggestion, the printf call expands to:

printf ("\n%d", ( ((1,2,3,4)*1) + ((1,2,3,4) * 2) +((1,2,3,4) * 3) + ((1,2,3,4) * 4) ) );

Syntactically correct, but obviously not what the OP wanted.

Dan
 
M

Martin Ambuhl

Naren said:
Hello,

Could anyone explain why I get an error when I compile this in MSVC6.0
compiler

The compiler should not be relevant.
#include <stdio.h>
#define PP 1,2,3,4
#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )
#define PP_Val PP_Func(PP)

PP_Func is defined as taking 4 arguments, and you are using it with one.
 
P

Peter Nilsson

pete said:
That's the same as

#define PP (1,2,3,4)

This is highly misleading. It's not the same at all.

The critical point to note is that PP is not substituted immediately
in subsequent macro _definitions_. Nor is it substituted immediately
on subsequent _usage_ of PP_Val [because it is not a macro parameter
to PP_Val.]
#define PP_Val PP_Func(PP, PP, PP, PP)

This won't do what I imagine the OP wants to do.

#include <stdio.h>

#define PP 1,2,3,4

#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) *
4) )

#define Val(PP) PP_Func(PP)

int main()
{
printf ("\n%d", Val(PP) );
return 0;
}

The expansion of Val(PP) within the printf is...

Val(PP) -> PP_Func(PP) -> PP_Func(1,2,3,4) -> ...

The obvious kludgy-ness of this would be good reason to rethink the
stratagy of defining a comma delimited parameter list as a macro.
 
Z

Zoran Cutura

Naren said:
Hello,

Could anyone explain why I get an error when I compile this in MSVC6.0
compiler

#include <stdio.h>

#define PP 1,2,3,4

#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )

#define PP_Val PP_Func(PP)

int main()

{

printf ("\n%d", PP_Val );

return 0;

}

Here we go... PP_Val is invoked, that is its text is replaced by the
text it is defined with. That is PP_Func(PP) will be inserted instead.
The preprocessor again checks if there are further prepocessor
invocations and it finds PP_Func to be invoked which is a functions like
mcaro and expects 4 arguments. But instead of four it is passed exactly
one argument which is PP, so it will give you some diagnostic.
 
N

Naren

#define PP 1,2,3,4
#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )
#define XPP_Func(arg) PP_Func(arg)
#define PP_Val XPP_Func(PP)

int main()
{
printf ("\n%d", PP_Val );
return 0;
}

When I compile this in MSVC6.0 I get this error
NewPreProcesor.c
f:\casc\newpre\newpreprocesor.c(8) : warning C4013: 'printf' undefined;
assuming extern returning int
f:\casc\newpre\newpreprocesor.c(8) : warning C4003: not enough actual
parameters for macro 'PP_Func'
f:\casc\newpre\newpreprocesor.c(8) : error C2059: syntax error : ')'>

Thanks in advance
Regards,
Naren.
 
P

Peter Shaggy Haywood

Groovy hepcat Naren was jivin' on Fri, 30 Jan 2004 19:16:43 +0530 in
comp.lang.c.
Re: Error in Preprocessing's a cool scene! Dig it!
When I compile this in MSVC6.0 I get this error
NewPreProcesor.c
f:\casc\newpre\newpreprocesor.c(8) : warning C4013: 'printf' undefined;
assuming extern returning int
f:\casc\newpre\newpreprocesor.c(8) : warning C4003: not enough actual
parameters for macro 'PP_Func'
f:\casc\newpre\newpreprocesor.c(8) : error C2059: syntax error : ')'>

Then you must have done something wrong, because the only diagnostic
message I get is this, which is very understandable:

testing.c(10): Warning! W301: No prototype found for 'printf'

(You goofed on this one, Dan.)
Naren, you must have (mis)typed the program into your programming
editor instead of copying and pasting. Had you done the latter, then
you would, no doubt, have seen the same results as I did.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
P

Paul Mensonides

Peter said:
Then you must have done something wrong, because the only diagnostic
message I get is this, which is very understandable:

testing.c(10): Warning! W301: No prototype found for 'printf'

(You goofed on this one, Dan.)
Naren, you must have (mis)typed the program into your programming
editor instead of copying and pasting. Had you done the latter, then
you would, no doubt, have seen the same results as I did.

Not necessarily. VC6 through the most current version fails to implement macro
replacement in the order defined by the standard. VC will fail this test every
time:

#define ARGS 1, 2

#define A(args) B(args)
#define B(x, y) x + y

A(ARGS)

Regards,
Paul Mensonides
 
D

Dan Pop

In said:
Groovy hepcat Naren was jivin' on Fri, 30 Jan 2004 19:16:43 +0530 in
comp.lang.c.
Re: Error in Preprocessing's a cool scene! Dig it!


Then you must have done something wrong, because the only diagnostic
message I get is this, which is very understandable:

testing.c(10): Warning! W301: No prototype found for 'printf'

(You goofed on this one, Dan.)

Are you reading impaired or what? From my previous post:

After making the change suggested above and removing the include line
(it is necessary for the program correctness, but not if the program is
only preprocessed, as it will generate tons of preprocessor output),
the preprocessor generates the following output:

Dan
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top