Thread: Defining a macro that takes any number of parameters?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Defining a macro that takes any number of parameters?

    Hi,
    I wanted to create a debug function that outputs extra info when compiled in debug mode, but compiles to nothing in release mode.
    I declared it like this in my header:
    Code:
    #ifndef DEBUG_H_INCLUDED_OCT_23_2007
    #define DEBUG_H_INCLUDED_OCT_23_2007
    
    #include <varargs.h>
    
    #define DEBUG_ALWAYS	1
    #define DEBUG_INFO	2
    #define DEBUG_WARNINGS	3
    #define DEBUG_ERRORS	4
    #define DEBUG_VERBOSE	5
    
    #if defined( DEBUG ) && defined( ANSI )
    	void DTRACE( va_alist );	/* ( int level, const char* fmt, ... ) */
    #else
    #	define DTRACE( a )
    #endif
    
    #endif	/* DEBUG_H_INCLUDED_OCT_23_2007 */
    However, when I compile in release mode I get errors like this:
    "macro "DTRACE" passed 5 arguments, but takes just 1"

    Is there a way to get around this?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In gcc you can use ... in the macro - this is not portable tho'.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    It's portable across C99 compilers.

    It's the function prototype that worries me. It uses the ancient, pre-standard varargs technique.

    You should #include <stdarg.h> (but only for the implementation) and have this prototype:
    Code:
    void DTRACE(int level, const char* fmt, ...);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Prior to C99, which makes this easier, you have to use the double parentheses trick.
    Code:
    #include <stdio.h>
    #include <stdarg.h>
    
    #if defined( DEBUG ) && defined( ANSI )
    void DTRACE_fn ( int level, const char* fmt, ... );
    #   define DTRACE( a )  DTRACE_fn a
    #else
    #   define DTRACE( a )
    #endif
    
    void DTRACE_fn ( int level, const char* fmt, ... ) {
        char temp[1000];
        va_list args;
        va_start( args, fmt );
        vsprintf( temp, fmt, args );
        printf("T: &#37;s\n", temp );
        va_end( args );
    }
    
    int main ( void ) {
        DTRACE((1,"Hello %s","world"));
        return 0;
    }
    The extra ( ) make it look like a single parameter to the macro, even though it has embedded commas.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program giving errors
    By andy bee in forum C Programming
    Replies: 5
    Last Post: 08-11-2010, 10:38 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  4. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM