macro doubt

Discussion in 'C Programming' started by sinbad, Dec 23, 2005.

  1. sinbad

    sinbad Guest

    is it possible to pass variable no.of arguments to a macro ...

    #define print(a,b) printf(a,b)

    void main()
    {
    int a=1,b=2,c=3;
    print("%d %d %d",(a,b,c));
    }

    the problem in the above code is the expression (a,b,c) evaluates to c
    ,
    and only the value of c will get printed.
    sinbad, Dec 23, 2005
    #1
    1. Advertising

  2. sinbad said:

    > is it possible to pass variable no.of arguments to a macro ...


    Not in standard C as currently implemented. If you are fortunate and unique
    enough to have a C99 compiler, use ... in the macro text and __VA_ARGS__ in
    the replacement.

    > #define print(a,b) printf(a,b)
    >
    > void main()


    main returns int.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 23, 2005
    #2
    1. Advertising

  3. sinbad wrote:
    > is it possible to pass variable no.of arguments to a macro ...


    The standard adopted in 1999 provides for this; the old standard from
    1989(1990) does not. Some compilers which otherwise conform to the
    1989(1990) standard have mechanisms for this; some look like the 1999
    standard mechanism and might be portable to a C99 implementation, but
    others use syntax or semantics differing from those adopted in the 199
    standard. So use these with care, reading the documentation carefully,
    and if you expect ever to use a 1989(1990) compiler with your code, then
    use them not at all.

    But ...

    > void main()

    ^^^^
    *NO* standard version of C (or the pre-standard K&R C) allows this for
    programs which will be compiled with an non-specific hosted
    implementation. If your implementation accepts this silently, remember
    that your next compiler (or even the next revision of your current one)
    is free to vomit at this non-standard return type.
    main returns an int in any C program portable across hosted implementations.
    Martin Ambuhl, Dec 23, 2005
    #3
  4. sinbad <> wrote:

    > is it possible to pass variable no.of arguments to a macro ...


    As stated, not in C89. It's possible in some cases to kludge your way
    around this limitation, however. For example,

    #define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}

    can be used like

    MY_ASSERT( 4 == 4, ("This will never be printed.") );

    and

    MY_ASSERT( some_var == 4, ("Error, some_var was not 4 (%u)",some_var) );

    Solutions of this sort may be useful but are not always stylistically
    acceptable. YMMV.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 23, 2005
    #4
  5. Christopher Benson-Manica <> wrote:

    > #define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}


    You may notice that I intended

    #define MY_ASSERT( cond, msg ) if( !(cond) ) {printf msg; abort();}

    Sorry.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 23, 2005
    #5
  6. sinbad

    Guest

    Christopher Benson-Manica schreef:

    > Christopher Benson-Manica <> wrote:
    >
    > > #define MY_ASSERT( cond, msg ) if( cond ) {printf msg; abort();}

    >
    > You may notice that I intended
    >
    > #define MY_ASSERT( cond, msg ) if( !(cond) ) {printf msg; abort();}


    Which will yield an error message from your compiler (if it's any good)

    Try

    #define MY_ASSERT( cond, msg ) \
    do { \
    if( !(cond) ) \
    { \
    fprintf(stderr, "ASSERTION FAILED [%s:%d] %s : %s\n", \
    __FILE__, __LINE__, #cond, msg}; abort(); \
    } \
    } while(0)

    Now your assert will work as an ordinary statement, that is, no
    superfluous ';' in the compilers input stream *and* it will provide the
    file and line position of the failed assertion, plus the condition that
    failed,
    plus any message you may want to provide.

    For the sake of debugging, it is also usefull to call some
    debug_whatever() function, so you can set a convenient breakpoint in
    your debugger.

    Besides, error messages belong on the stderr stream.
    , Dec 23, 2005
    #6
  7. wrote:

    > Now your assert will work as an ordinary statement, that is, no
    > superfluous ';' in the compilers input stream *and* it will provide the
    > file and line position of the failed assertion, plus the condition that
    > failed,
    > plus any message you may want to provide.


    Thanks for the correction, and no, my compiler is not any good, so I'm
    not particularly surprised that it isn't emitting warnings on similar
    code.

    > Besides, error messages belong on the stderr stream.


    Aye.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 23, 2005
    #7
  8. Richard Heathfield a écrit :
    >>is it possible to pass variable no.of arguments to a macro ...

    >
    > Not in standard C as currently implemented. If you are fortunate and unique
    > enough to have a C99 compiler, use ... in the macro text and __VA_ARGS__ in


    Just need a recent distribution of Linux including gcc or a mingw
    compiler on Windows (like the one coming with Dev-C++ or better
    Code::Blocks).

    --
    A+

    Emmanuel Delahaye
    Emmanuel Delahaye, Dec 23, 2005
    #8
  9. Emmanuel Delahaye said:

    > Richard Heathfield a écrit :
    >>>is it possible to pass variable no.of arguments to a macro ...

    >>
    >> Not in standard C as currently implemented. If you are fortunate and
    >> unique enough to have a C99 compiler, use ... in the macro text and
    >> __VA_ARGS__ in

    >
    > Just need a recent distribution of Linux including gcc or a mingw
    > compiler on Windows (like the one coming with Dev-C++ or better
    > Code::Blocks).


    ....provided you are prepared to lower your diagnostics level, or use the
    misleading "c99" switch. I am not prepared to do either. Note that I said
    "not in >>>standard<<< C as currently implemented".

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 24, 2005
    #9
  10. sinbad

    Guest

    Christopher Benson-Manica schreef:

    > wrote:
    >
    > > Now your assert will work as an ordinary statement, that is, no
    > > superfluous ';' in the compilers input stream *and* it will provide the
    > > file and line position of the failed assertion, plus the condition that
    > > failed,
    > > plus any message you may want to provide.

    >
    > Thanks for the correction, and no, my compiler is not any good, so I'm
    > not particularly surprised that it isn't emitting warnings on similar
    > code.


    My sincere condoleances. I've been forced to use a crappy compiler on
    one occasion, so i know what a pain that can be. No option to switch
    compilers?

    > > Besides, error messages belong on the stderr stream.

    >
    > Aye.
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    , Dec 24, 2005
    #10
  11. [OT] (was: Re: macro doubt)

    wrote:

    > My sincere condoleances. I've been forced to use a crappy compiler on
    > one occasion, so i know what a pain that can be. No option to switch
    > compilers?


    No. What makes the situation particularly frustrating is that the
    vendor makes a more current version freely available, but there are no
    resources to move the codebase to that compiler.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Dec 27, 2005
    #11
    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. Dead RAM
    Replies:
    20
    Views:
    1,111
    John Harrison
    Jul 14, 2004
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    623
  3. Replies:
    0
    Views:
    559
  4. Peter Otten
    Replies:
    2
    Views:
    111
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    115
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page