Results 1 to 3 of 3

Thread: variadic macros

  1. #1
    Join Date
    Mar 2011
    Location
    London,.UK
    Beans
    21

    variadic macros

    Hi

    I am hoping to use the ellipsis in macros with gcc compiling C code. Wikipedia says its OK, but I get failure.

    My hope is to provide an interface between code for stdio.h and ncurses. For instance I desire :

    Code:
    #define fprintf( A , ... ) ( A == stdout ? printw ( ... ) else fprintf ( A , ... ) )
    For testing I write this :

    Code:
    /*    variadic.c
          test variadic macros        */
    
    #include <stdio.h>
    
    #define show( ... ) printf ( ... )
    
    int main ( )
    { show( "%d  HARROW %s\n" , 1 , "WEALD" ) ; return 0 ; }
    The response is :

    variadic.c:9: error: expected expression before ‘...’ token
    Thanks for your interest, I would appreciate any advice.

  2. #2
    Join Date
    Jun 2008
    Location
    Paris
    Beans
    260

    Re: variadic macros

    Quote Originally Posted by Alan5 View Post
    Hi

    I am hoping to use the ellipsis in macros with gcc compiling C code. Wikipedia says its OK, but I get failure.

    My hope is to provide an interface between code for stdio.h and ncurses. For instance I desire :

    Code:
    #define fprintf( A , ... ) ( A == stdout ? printw ( ... ) else fprintf ( A , ... ) )
    For testing I write this :

    Code:
    /*    variadic.c
          test variadic macros        */
    
    #include <stdio.h>
    
    #define show( ... ) printf ( ... )
    
    int main ( )
    { show( "%d  HARROW %s\n" , 1 , "WEALD" ) ; return 0 ; }
    The response is :

    Thanks for your interest, I would appreciate any advice.

    usually, GCC is more right than Wikipedia about GCC. http://gcc.gnu.org/onlinedocs/gcc-4....ic-Macros.html

    try this:
    Code:
    #define my_fprintf( A , ... ) ( if (A == stdout) printw ( __VA_ARGS__ ) else fprintf ( A , __VA_ARGS__  ) )
    by the way you mixed the cond ? foo : bar and the if (cond) then foo; else bar; , so even if you got the correct syntax for variadic macros, you were quite far from it

    oh and I think that overloading symbols with macros is quite ill-advised...

    br,

    Mathieu

  3. #3
    Join Date
    Mar 2011
    Location
    London,.UK
    Beans
    21

    Re: variadic macros

    Brilliant, many thanks Mathieu.

    __VA_ARGS__ works perfectly with the little test.

    And many thanks for the link to the gcc documentation. I have spent days hunting for the answer using Google and never found this primary resource.

    Alan

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •