473,412 Members | 2,072 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,412 software developers and data experts.

preprocessor and parenthesis

hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.

my question(s):
o is there a better way of passing a comma separated list as the 'b'
parameter
o is gcc being generous by
a) preprocessing this
b) compiling it
o how can i write a macro like this:

#define F(a,b) int a(obj*o b

so that i can write function declarations in which all functions have a
first param obj, but may optionally have more.

thanks,
jack
ivorykite.com

Feb 6 '06 #1
5 2003
effbiae wrote:
hi,
Hi,
this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})
How about moving the } to the macro definition?

#define M(a,b) static int is[3]={a,b};
M(1,(2,3))

Isn't that more readable?
produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.
gcc 3.4 and 4 do complain about that no matter what, and older versions
complain about it with -pedantic. Additionally, on those older
versions, it probably doesn't do what you expect: it initialises is[0]
with 1, and is[1] with (2,3), which evaluates to 3.
my question(s):
o is there a better way of passing a comma separated list as the 'b'
parameter
Variadic macros.

#define M(...) static int is[3]={__VA_ARGS__};
M(1,2,3)

Or, if you want to enforce a minimum of two arguments:

#define M(a, ...) static int is[3]={a, __VA_ARGS__};
M(1,2,3)
o is gcc being generous by
a) preprocessing this
Nope.
b) compiling it
Yep.
o how can i write a macro like this:

#define F(a,b) int a(obj*o b

so that i can write function declarations in which all functions have a
first param obj, but may optionally have more.


Again, why not put the ) in the macro definition?

#define F(a,b) int a(obj*o b)

gcc has a non-standard extension that makes this easy -- refer to the
documentation if you're interested -- but with standard C it, while
possible, is needlessly complex since you don't know in advance whether
you'll need a comma after o. The easiest route is to create two macros:

#define F_0(a) int a(obj* o)
#define F_1(a,...) int a(obj* o, __VA_ARGS__)

and choose the appropriate one by hand each time.

HTH

Feb 6 '06 #2
Harald wrote:

thanks for your help.
Variadic macros.
of course...
you'll need a comma after o. The easiest route is to create two macros:

#define F_0(a) int a(obj* o)
#define F_1(a,...) int a(obj* o, __VA_ARGS__)

and choose the appropriate one by hand each time.


i think i could do something like this:
#define F(a) int a(obj*o,...)
and leave the argument-getting up to the function

do i pay an efficiency penalty for using va_list macros/functions?

thanks,

jack

Feb 6 '06 #3
effbiae wrote:
i think i could do something like this:
#define F(a) int a(obj*o,...)
and leave the argument-getting up to the function
Yeah, that should work. You may now accidentally call the functions
with the wrong number of arguments, of course, so be careful about
that.
do i pay an efficiency penalty for using va_list macros/functions?


In theory, yes, but it's nothing likely to be noticeable. If it makes
things easier, go for it.

Feb 7 '06 #4

effbiae wrote:
hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.

my question(s):
o is there a better way of passing a comma separated list as the 'b'
parameter
o is gcc being generous by
a) preprocessing this
b) compiling it
o how can i write a macro like this:

#define F(a,b) int a(obj*o b

so that i can write function declarations in which all functions have a
first param obj, but may optionally have more.

thanks,
jack
ivorykite.com


Hello,

If all the others posibilities fails, you can always use an external
and more powerful preprocessor, like "m4".

Kind regards.

Feb 7 '06 #5
On 2006-02-06, effbiae <ef*****@ivorykite.com> wrote:
hi,

this code:

#define M(a,b) static int is[3]={a,b;
M(1,(2,3)})

produces this (using gcc -E):

static int is[3]={1,(2,3)};

which gcc does not complain about.


I believe this statement is equivalent to
static int is[3] = {1,3,0};
Feb 7 '06 #6

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
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....
38
by: Steve Kirsch | last post by:
I need a simple function that can match the number of beginning and ending parenthesis in an expression. Here's a sample expression: ( ( "john" ) and ( "jane" ) and ( "joe" ) ) Does .NET have...
4
by: Oudenhoven, Timothy L | last post by:
Is there a way to turn off the need to use parenthesis in queries for tablenames etc. I have seen in some documentation examples that parenthesis are not used in the queries. In my install of...
15
by: Christopher Benson-Manica | last post by:
Is it possible to write a macro (in unextended C89) such that TEST( int, (1,2,3) ); expands to int array={ 1,2,3 }; ? I strongly suspect that it is not, but I don't wish to overlook a...
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...
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...
9
by: wenmang | last post by:
Hi, all: I have a question regarding to how to solve following problem: I have header called myHeader.h which #define MAX_LEN 100 (legacy code). Now, I like to put most commonly used in...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.