8

I have a bunch of printf debug helper macros and it would be pretty cool to have to not specify the type, is there anything you can do to allow something like macro overloading in c(can be gcc specific if its available in gcc 4.3). I thought maybe typeof but apparently that doesn't work.

example macro(I also have some ascii terminal color stuff that I can't remember of the top of my head)

#ifdef _DEBUG
#define DPRINT_INT(x) printf("int %s is equal to %i at line %i",#x,x,__LINE__);
.
.
.
#else
#define DPRINT_INT(x)
.
.
.
#endif
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
Roman A. Taycher
  • 18,619
  • 19
  • 86
  • 141

3 Answers3

9

Try this; it uses gcc's __builtin methods, and automatically determines the type for you, as best it can, and makes for an easy DEBUG macro where you don't have to specify the type. Of course, you can compare typeof (x) to float, etc. etc.

#define DEBUG(x)                                                 \
  ({                                                             \
    if (__builtin_types_compatible_p (typeof (x), int))          \
        fprintf(stderr,"%d\n",x);                                \
    else if (__builtin_types_compatible_p (typeof (x), char))    \
        fprintf(stderr,"%c\n",x);                                \
    else if (__builtin_types_compatible_p (typeof (x), char[]))  \
        fprintf(stderr,"%s\n",x);                                \
    else                                                         \
        fprintf(stderr,"unknown type\n");                        \

  })
Ben Stott
  • 2,218
  • 17
  • 23
  • Yay, That looks perfect. Thanks, wonders why more people don't use it(runtime overhead)? – Roman A. Taycher Apr 12 '11 at 10:51
  • 1
    Cause it's limited to GCC and people might want code to be compiled with other compilers as well. – Mario Apr 12 '11 at 10:52
  • Runtime overhead is useless though - it's for a debugging build anyway, so this will be #define'd out during a regular, production build. It's probably not widely used because it's gcc-specific – Ben Stott Apr 12 '11 at 10:53
  • Now that I see it I started imagining how it could be used to do somewhat safe type overloading without long names even in regular code. As for gcc specific a lot of unix software these days is probably somewhat gcc specific(although that mught change due to llvm). – Roman A. Taycher Apr 12 '11 at 11:02
  • Some, sure, but not _all_, which is why this won't be so widely used. – Ben Stott Apr 12 '11 at 11:04
1

How about the following macro? It still requires you to pick the print format but you won't have to redefine all possible cases and it works on MSVC as well:

#define DPRINT(t,v) printf("The variable '%s' is equal to '%" ## #t ## "' on line %d.\n",#v,v, __LINE__)

To use it:

int var = 5;
const char *str = "test";
DPRINT(i,var);
DPRINT(s,str);
Mario
  • 35,726
  • 5
  • 62
  • 78
0

I think you can give a try with following code:

#define DPRINT(fmt) do{ \
            my_printf fmt ; \
        } while(0)

my_printf( const char* szFormat, ... )
{
    char szDbgText[100];

    va_start(args, szFormat);
    vsnprintf(&szDbgText,99,szFormat,args);
    va_end(args);

    fprintf( stderr, "%s", szDbgText );
}
// usage
func( )
{
    int a = 5;
    char *ptr = "hello";

    DPRINT( ("a = %d, ptr = %s\n", a, ptr) );
}

Note: DPRINT usage takes dual parentheses here.

rajiv.78
  • 1
  • 1