Thread: A tricky macro

  1. #1
    Unregistered
    Guest

    Angry A tricky macro

    Ok itz atleast tricky to me!

    I want to write a macro which will do something
    like

    #define PRINTF(x) printf x

    :
    somewhere in code
    :
    PRINTF(("This is a zero %d\n", 0));
    :
    :

    Works fine until now.

    Now I need to make it something like

    #define PRINTFX(x) \
    fnCall(someparam, x)

    Cant get this to work! Please note that x denotes variable number of parameters like printf.

    The intended use is like
    PRINTFX("One", "This is one %d\n", 1);
    PRINTFX("Two", "Nothing);

    Can add any number of paranthesis to the parameters.

    Thanks for any help, nope this is not a brain teaser, it would be cool if I could make this work ... else the good old way, multiple functions with variable number of parameters to a function.

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    i don't think it's possible with #defines. printf is a weird function, though. it doesn't use multiple prototypes.
    http://www.purdue.edu/PUCC/Short-Cou...s/p_00930.html
    this should help answer questions

    Edit: i read your post again and realized you knew this already. i don't think it's possible with #define. can't overload a define, nor can you just put it all in parenthesis without causing problems.
    Last edited by ygfperson; 02-12-2002 at 05:09 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    This might work for you, but you still have to use the (()) trick for all the additional parameters

    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    #define PRINTFX(x,y) \
        fnCall( x, magic y ) 
    
    void fnCall ( char *x, char *y ) {
      printf( "%s %s\n", x, y );
    }
    char *magic ( const char *fmt, ... ) {
      static char buff[BUFSIZ]; // must be static
      va_list   ap;
      va_start ( ap, fmt );
      vsprintf ( buff, fmt, ap );
      va_end ( ap );
      return buff;
    }
    
    int main ( ) {
      PRINTFX( "One", ("This is one %d\n",1) ); 
      PRINTFX( "Two", ("Nothing") ); 
      return 0;
    }

  4. #4
    Unregistered
    Guest

    Thumbs up

    Thanks! That worked. Sorry I was tardy in getting back to the list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  4. need help with multi line macro usage
    By Cdigitproc1 in forum C Programming
    Replies: 9
    Last Post: 04-29-2005, 09:50 AM
  5. Tricky #define macro...
    By willkoh in forum Windows Programming
    Replies: 4
    Last Post: 04-06-2005, 12:09 PM