473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Error in Preprocessing

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.

Nov 14 '05 #1
11 1349
Naren wrote:

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)
int main()

{

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

return 0;

}

--
pete
Nov 14 '05 #2
In <bv**********@ns2.fe.internet.bosch.com> "Naren" <na*************@in.bosch.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #3
In <40***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Naren wrote:

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)


Nonsense.
#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)


More nonsense.
int main()

{

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

return 0;

}


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4
Naren wrote:
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.
int main()
{
printf ("\n%d", PP_Val );
return 0;
}

Thanks in advance,


--
Martin Ambuhl
Nov 14 '05 #5
pete <pf*****@mindspring.com> wrote in message news:<40***********@mindspring.com>...
Naren wrote:

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)


This is highly misleading. It's not the same at all.
#define PP_Func(x,y,z,w) ( ((x)*1) + ((y) * 2) +((z) * 3) + ((w) * 4) )

#define PP_Val PP_Func(PP)
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.
int main()

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


#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.

--
Peter
Nov 14 '05 #6
Dan Pop wrote:

In <40***********@mindspring.com> pete <pf*****@mindspring.com> writes:
Naren wrote:

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)


Nonsense.


Thank you.

--
pete
Nov 14 '05 #7
> #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.
Nov 14 '05 #8
Naren <na*************@in.bosch.com> wrote:
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.

--
Z (Zo**********@daimlerchrysler.com)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #9
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!
#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 : ')'>


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"?
Nov 14 '05 #10
Peter "Shaggy" Haywood wrote:
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.


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
Nov 14 '05 #11
In <40***************@news.australis.net> sh****@australis.net.STOP.SPAM (Peter "Shaggy" Haywood) writes:
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!
#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 : ')'>


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
1
by: Alex Sedow | last post by:
Where to get/buy tool for partial preprocessing? 1. What I mean by partial macro preprocessing. This is mode in which almost sources (>99%) is not preprocessed. But some macros (setted by...
12
by: Francois Grieu | last post by:
Can #include safely use a preprocessing token, as in #define HEADERFILE "stdio.h" #include HEADERFILE int main(void) {return printf("Hello, world\n")*0;} TIA, François Grieu
5
by: cody | last post by:
the following leads to an error (note that TEST is not defined): #if TEST string s = @" #"; // <-- the error is "preprocessing directive expected" #endif also, here we get the same error: ...
4
by: Henrik Goldman | last post by:
I have an application which compiles on a number of different platforms. Now I'm trying to incorporate an external library which is only available to a subset of the platforms that my application...
14
by: Eliot | last post by:
I have a project which may be compiled with some #defines. Say:- #define firstdef #define seconddef If no defines are present or only one is present all is OK. I would like to add a self...
14
by: yeah | last post by:
hi all I am working with linux environment and i compiled device driver programs using native compiler "gcc".In one of my macro i have these lines #if (1 << HARDIRQ_BITS) < MAX_HARDIRQS_PER_CPU...
5
by: Francois Grieu | last post by:
One of the C compiler that I use <OT>(Keil's CX51)</OTbarks at #define a(b) b int main(void){return a( #if 0 #endif 0);} More generally, this compiler seems confused by any preprocessing...
40
by: Bill Cunningham | last post by:
I have been thinking about hiding headers from my compiler's preprocessor and allowing others to be shown. I want to use the most used and add others is necessary. Would this be how it is properly...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.