preprocessor and parenthesis

Discussion in 'C Programming' started by effbiae, Feb 6, 2006.

  1. effbiae

    effbiae Guest

    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
    effbiae, Feb 6, 2006
    #1
    1. Advertising

  2. 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
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 6, 2006
    #2
    1. Advertising

  3. effbiae

    effbiae Guest

    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
    effbiae, Feb 6, 2006
    #3
  4. 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.
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Feb 7, 2006
    #4
  5. effbiae

    tmp123 Guest

    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.
    tmp123, Feb 7, 2006
    #5
  6. effbiae

    Jordan Abel Guest

    On 2006-02-06, 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.


    I believe this statement is equivalent to
    static int is[3] = {1,3,0};
    Jordan Abel, Feb 7, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cronus
    Replies:
    1
    Views:
    634
    Paul Mensonides
    Jul 15, 2004
  2. IveCal
    Replies:
    4
    Views:
    373
    stefanomnn
    Jun 22, 2007
  3. hakunin
    Replies:
    30
    Views:
    374
    Dave Bass
    Jun 19, 2008
  4. Daniel Fac
    Replies:
    3
    Views:
    130
    Daniel Fac
    Nov 2, 2008
  5. Carl Cunningham

    Regular expressions and parenthesis in match text

    Carl Cunningham, Sep 15, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    102
    Carl Cunningham
    Sep 15, 2003
Loading...

Share This Page