16

As far as I know, in gcc you can write something like:

#define DBGPRINT(fmt...) printf(fmt);

Is there a way to do that in VC++?

a3f
  • 8,517
  • 1
  • 41
  • 46
Domo
  • 163
  • 1
  • 2
  • 6

9 Answers9

24

Yes but only since VC++ 2005. The syntax for your example would be:

#define DBGPRINT(fmt, ...) printf(fmt, __VA_ARGS__)

A full reference is here.

Gareth Simpson
  • 36,943
  • 12
  • 47
  • 50
4

Yes, you can do this in Visual Studio C++ in versions 2005 and beyond (not sure about VS 2003). Take a look at VA_ARGS. You can basically do something like this:

#define DBGPRINTF(fmt, ...)  printf(fmt, __VA_ARGS__)

and the variable arguments to the macro will get passed to the function provided as '...' args, where you can then us va_args to parse them out.

There can be weird behavior with VA_ARGS and the use of macros. Because VA_ARGS is variable, that means that there can be 0 arguments. That might leave you with trailing commas where you didn't intend.

Mark
  • 10,022
  • 2
  • 38
  • 41
4

If you do not want to use non-standard extensions, you've to provide extra brackets:

#define DBGPRINT(args) printf(args);
DBGPRINT(("%s\n", "Hello World"));
yrp
  • 4,565
  • 2
  • 24
  • 10
  • 4
    This is so wrong, and yet got upvotes ... I would fire someone for the semicolon at the end of the define alone. What you intended: `#define DBGPRINT(args) printf args` ... but for this case, just `#define DBGPRINT printf` will do. – Jim Balter Oct 26 '12 at 01:11
  • #define DBGPRINT printf will not do if you want to elide all of your debugging output code from the executable. You will be left with all (...) parameter lists and it will at least trigger warnings, may cause errors and unexpected side effects. – Tomek Sep 04 '14 at 13:32
1

For MSVC 7.1 (.NET 2003), this works:

#if defined(DETAILED_DEBUG)
#define DBGPRINT fprintf
#else
__forceinline void __DBGPRINT(...){}
#define DBGPRINT __DBGPRINT
#endif
amarcruz
  • 11
  • 1
1

What you're looking for are called [variadic macros](http://msdn.microsoft.com/en-us/library/ms177415(VS.80).aspx).

Summary of the link: yes, from VC++ 2005 on up.

1

If you don't actually need any of the features of macros (__FILE__, __LINE__, token-pasting, etc.) you may want to consider writing a variadic function using stdargs.h. Instead of calling printf(), a variadic function can call vprintf() in order to pass along variable argument lists.

bk1e
  • 23,871
  • 6
  • 54
  • 65
0

The following should work. (See link to Variadic macros)

(Example below shows a fixed and variable arguments.)

#  define DBGPRINTF(fmt,...) \
    do { \
        printf(fmt, __VA_ARGS__); \
    } while(0)
David Dolson
  • 300
  • 2
  • 8
0

Search for "VA_ARGS" and va_list in MSDN!

James
  • 1,973
  • 1
  • 18
  • 32
-2

Almost. It's uglier than that though (and you probably don't want a trailing semi-colon in the macro itself:

#define DBGPRINT(DBGPRINT_ARGS) printf DBGPRINT_ARGS // note: do not use '(' & ')'

To use it:

DBGPRINT(("%s\n", "Hello World"));

(was missing a pair of parens).

Not sure why all the negatives, the original question didn't state a version of VC++, and variadic macros aren't supported by all compilers.

qwertz
  • 14,614
  • 10
  • 34
  • 46
kfh
  • 321
  • 1
  • 6
  • Persumably the downvotes were because the missing parens meant it was broken ... though remarkably, an even more wrong version of this up above got 5 upvotes. All of you folks missed the simple `#define DBGPRINT printf` – Jim Balter Oct 26 '12 at 01:17