473,387 Members | 1,834 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,387 software developers and data experts.

C tricky problem or gcc preprocessor bug ???

Hi,

Compilation of the below code fails
cat test.c
#define a this1, /* Comma is intentional line 1*/

#define fun( x, y, z) y*x*z/*line 2*/

char this1,b,c;/*line 3*/
int main()/*line 4*/
{
fun(b, a c); /*Comma is leftout intetionaly line 5*/
}/*line 6*/

with errors as below
gcc test.c
test.c:8:15: macro "fun" requires 3 arguments, but only 2 given
test.c: In function `main':
test.c:8: error: `fun' undeclared (first use in this function)
test.c:8: error: (Each undeclared identifier is reported only once
test.c:8: error: for each function it appears in.)
If I just change line 1 and line 5 as below it works obviously.
I want to compile the original above code as it is just a snippet of
my project ! Please help.

"#define a this1," to "#define a this1"
"fun(b, a c);" to "fun(b, a, c);" (as below)then it works fine.
cat test.c
#define a this1, /*line 1*/

#define fun( x, y, z) y*x*z/*line 2*/

char this1,b,c;/*line 3*/
int main()/*line 4*/
{
fun(b, a c);/*line 5*/
}/*line 6*/

Jul 3 '07 #1
6 1991
On Jul 3, 11:18 am, Subra <mailursu...@gmail.comwrote:
Hi,

Compilation of the below code fails
cat test.c

#define a this1, /* Comma is intentional line 1*/

#define fun( x, y, z) y*x*z/*line 2*/

char this1,b,c;/*line 3*/
int main()/*line 4*/
{
fun(b, a c); /*Comma is leftout intetionaly line 5*/

}/*line 6*/

with errors as belowgcc test.c

test.c:8:15: macro "fun" requires 3 arguments, but only 2 given
test.c: In function `main':
Macro arguments are not expanded until after the macro itself is
expanded. When the "fun" macro is expanded only 2 arguments are
provided instead of the required 3 which is a constraint violation.

Robert Gamble

Jul 3 '07 #2
Macro arguments are not expanded until after the macro itself is
expanded. When the "fun" macro is expanded only 2 arguments are
provided instead of the required 3 which is a constraint violation.
Any preprocessor option to avoid this ???

Jul 4 '07 #3
Subra <ma*********@gmail.comwrites:
>Macro arguments are not expanded until after the macro itself is
expanded. When the "fun" macro is expanded only 2 arguments are
provided instead of the required 3 which is a constraint violation.
The above was written by Robert Gamble. Please don't delete
attribution lines.
Any preprocessor option to avoid this ???
I doubt it.

The C standard doesn't specify "options" to the preprocessor or to the
compiler; it merely specifies how the compiler is required to behave.
You wrote a macro that requires 3 arguments, and you passed it only 2
arguments. That's a constraint violation, and a diagnostic is
required; in most implementations, the compilation will be rejected.

The solution is to change the macro so it requires 2 arguments, or to
change the invocation so it passes 3 arguments.

C99 introduces variadic macros; your compiler may or may not support
them. But I don't know if that's really what you're looking for.

What are you really trying to do?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 4 '07 #4
On Jul 4, 7:05 am, Keith Thompson <k...@mib.orgwrote:
Subra <mailursu...@gmail.comwrites:
Macro arguments are not expanded until after the macro itself is
expanded. When the "fun" macro is expanded only 2 arguments are
provided instead of the required 3 which is a constraint violation.
Any preprocessor option to avoid this ???

I doubt it.

The C standard doesn't specify "options" to the preprocessor or to the
compiler; it merely specifies how the compiler is required to behave.
You wrote a macro that requires 3 arguments, and you passed it only 2
arguments. That's a constraint violation, and a diagnostic is
required; in most implementations, the compilation will be rejected.

The solution is to change the macro so it requires 2 arguments, or to
change the invocation so it passes 3 arguments.

C99 introduces variadic macros; your compiler may or may not support
them. But I don't know if that's really what you're looking for.

What are you really trying to do?
I have third party library which was taking 2 args for a macro as
below.

_RW_VAL_ALLOC(a,b) a*b // It is used by platforms HPUX, Solaris

Now the new version of library takes 3 args

_RW_VAL_ALLOC(a,c,b) c*a*b // It is used by LINUX

My code which makes the call to _RW_VAL_ALLOC have to select different
versions
of the macro depending on the platfrom

So my code is like below

#if defined(NEW_VERSION_RW)
#define PIN_ALLOC_VAL argPresnt,
#else
#define PIN_ALLOC_VAL
#endif

int main()
{
int a, b, argPresent;
_RW_VAL_ALLOC(a,
PIN_ALLOC_VAL // Only when we use the new version of
lib, it should get
// replaced by a arg.
b);

}

This solution is not working.

Jul 4 '07 #5
On Jul 4, 12:21 am, Subra <mailursu...@gmail.comwrote:
On Jul 4, 7:05 am, Keith Thompson <k...@mib.orgwrote:
Subra <mailursu...@gmail.comwrites:
>Macro arguments are not expanded until after the macro itself is
>expanded. When the "fun" macro is expanded only 2 arguments are
>provided instead of the required 3 which is a constraint violation.
Any preprocessor option to avoid this ???
I doubt it.
The C standard doesn't specify "options" to the preprocessor or to the
compiler; it merely specifies how the compiler is required to behave.
You wrote a macro that requires 3 arguments, and you passed it only 2
arguments. That's a constraint violation, and a diagnostic is
required; in most implementations, the compilation will be rejected.
The solution is to change the macro so it requires 2 arguments, or to
change the invocation so it passes 3 arguments.
C99 introduces variadic macros; your compiler may or may not support
them. But I don't know if that's really what you're looking for.
What are you really trying to do?

I have third party library which was taking 2 args for a macro as
below.

_RW_VAL_ALLOC(a,b) a*b // It is used by platforms HPUX, Solaris

Now the new version of library takes 3 args

_RW_VAL_ALLOC(a,c,b) c*a*b // It is used by LINUX

My code which makes the call to _RW_VAL_ALLOC have to select different
versions
of the macro depending on the platfrom

So my code is like below

#if defined(NEW_VERSION_RW)
#define PIN_ALLOC_VAL argPresnt,
#else
#define PIN_ALLOC_VAL
#endif

int main()
{
int a, b, argPresent;
_RW_VAL_ALLOC(a,
PIN_ALLOC_VAL // Only when we use the new version of
lib, it should get
// replaced by a arg.
b);

}

This solution is not working.
How about this:

<==CUT==>
#if defined(NEW_VERSION_RW)
#define MY_RW_VAL_ALLOC(a, c, b) _RW_VAL_ALLOC(a, c, b)
#else
#define MY_RW_VAL_ALLOC(a, c, b) _RW_VAL_ALLOC(a, b)
#endif

MY_RW_VAL_ALLOC(a, c, b)
<==CUT==>

$ gcc -E test.c
_RW_VAL_ALLOC(a, b)

$ gcc -E -DNEW_VERSION_RW test.c
_RW_VAL_ALLOC(a, c, b)

Robert Gamble

Jul 4 '07 #6
On Jul 4, 9:32 am, Robert Gamble <rgambl...@gmail.comwrote:
On Jul 4, 12:21 am, Subra <mailursu...@gmail.comwrote:
On Jul 4, 7:05 am, Keith Thompson <k...@mib.orgwrote:
Subra <mailursu...@gmail.comwrites:
Macro arguments are not expanded until after the macro itself is
expanded. When the "fun" macro is expanded only 2 arguments are
provided instead of the required 3 which is a constraint violation.
Any preprocessor option to avoid this ???
I doubt it.
The C standard doesn't specify "options" to the preprocessor or to the
compiler; it merely specifies how the compiler is required to behave.
You wrote a macro that requires 3 arguments, and you passed it only 2
arguments. That's a constraint violation, and a diagnostic is
required; in most implementations, the compilation will be rejected.
The solution is to change the macro so it requires 2 arguments, or to
change the invocation so it passes 3 arguments.
C99 introduces variadic macros; your compiler may or may not support
them. But I don't know if that's really what you're looking for.
What are you really trying to do?
I have third party library which was taking 2 args for a macro as
below.
_RW_VAL_ALLOC(a,b) a*b // It is used by platforms HPUX, Solaris
Now the new version of library takes 3 args
_RW_VAL_ALLOC(a,c,b) c*a*b // It is used by LINUX
My code which makes the call to _RW_VAL_ALLOC have to select different
versions
of the macro depending on the platfrom
So my code is like below
#if defined(NEW_VERSION_RW)
#define PIN_ALLOC_VAL argPresnt,
#else
#define PIN_ALLOC_VAL
#endif
int main()
{
int a, b, argPresent;
_RW_VAL_ALLOC(a,
PIN_ALLOC_VAL // Only when we use the new version of
lib, it should get
// replaced by a arg.
b);
}
This solution is not working.

How about this:

<==CUT==>
#if defined(NEW_VERSION_RW)
#define MY_RW_VAL_ALLOC(a, c, b) _RW_VAL_ALLOC(a, c, b)
#else
#define MY_RW_VAL_ALLOC(a, c, b) _RW_VAL_ALLOC(a, b)
#endif

MY_RW_VAL_ALLOC(a, c, b)
<==CUT==>

$ gcc -E test.c
_RW_VAL_ALLOC(a, b)

$ gcc -E -DNEW_VERSION_RW test.c
_RW_VAL_ALLOC(a, c, b)

Robert Gamble
I will be using the above fix ! Thank you Robert.

Jul 4 '07 #7

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

Similar topics

205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
24
by: Nudge | last post by:
I have an array, and an unrolled loop which looks like this: do_something(A); do_something(A); .... do_something(A); I thought: why should I type so much? I should write a macro. So I was...
13
by: Chris Croughton | last post by:
Is the following code standard-compliant, and if so what should it do? And where in the standard defines the behaviour? #include <stdio.h> #define DEF defined XXX int main(void) { int...
9
by: Walter Roberson | last post by:
I have run into a peculiarity with SGI's C compiler (7.3.1.2m). I have been reading carefully over the ANSI X3.159-1989 specification, but I cannot seem to find a justification for the behaviour....
3
by: Dave | last post by:
So I'm trying to write a CSS preprocessor. I want to add the ability to append a selector onto other selectors. So, given the following code: ========================================= #selector...
32
by: spibou | last post by:
Is the output of the C preprocessor deterministic ? What I mean by that is , given 2 compilers which conform to the same standard, will their preprocessors produce identical output given as input...
6
by: Adrian Hawryluk | last post by:
Does anyone know of a way to manipulate a macro list to remove either the beginning or the end element? I.e. Given: #define ELEMENTS EL(1) EL(2) EL(3) Is there any manipulation to get just...
31
by: Sam of California | last post by:
Is it accurate to say that "the preprocessor is just a pass in the parsing of the source file"? I responded to that comment by saying that the preprocessor is not just a pass. It processes...
6
by: Michael B Allen | last post by:
Hi, I have a macro that looks like the following: #define MMSG msgno_loc0(LOC0, LOC1) && msgno_mmsg0 which if used in some code like: MMSG("foo=%s", foo);
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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,...

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.