473,698 Members | 2,556 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2006
On Jul 3, 11:18 am, Subra <mailursu...@gm ail.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*********@gm ail.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_Keit h) 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.orgwr ote:
Subra <mailursu...@gm ail.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_VER SION_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...@gm ail.comwrote:
On Jul 4, 7:05 am, Keith Thompson <k...@mib.orgwr ote:
Subra <mailursu...@gm ail.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_VER SION_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_VER SION_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...@gmai l.comwrote:
On Jul 4, 12:21 am, Subra <mailursu...@gm ail.comwrote:
On Jul 4, 7:05 am, Keith Thompson <k...@mib.orgwr ote:
Subra <mailursu...@gm ail.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_VER SION_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_VER SION_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
10638
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
40221
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 looking to write something along the lines of:
13
2128
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 defined = 2;
9
3656
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. Could someone point me to the appropriate section, or else confirm the behaviour as a bug? For a particular project, I am using the C preprocessor phase only. I am not using the standalone program 'cpp' because proper functioning of my project...
3
1185
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 { { property: value; property: value; } .other_selector { property: value; property: value; }
32
2784
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 the same file ? If not then how much variation is allowed ? Is it just a bit more or less white space here and there or could could there be larger differences ? If the output is not deterministic then is it possible that the output of the...
6
3565
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 EL(1) EL(2) out of ELEMENTS? Thanks for your help.
31
2913
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 statements that the compiler does not process. The good people in the alt.comp.lang.learn.c-c++ newsgroup insist that the preprocessor is just one of many passes. The preprocessor processes a grammer unique to the preprocessor and only that grammer. ...
6
2418
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
9170
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9031
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8901
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3052
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.