Jump to content

Talk:Variadic macro in the C preprocessor: Difference between revisions

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia
Content deleted Content added
Markhurd (talk | contribs)
→‎fewer arguments: MSC drops the trailing comma
No edit summary
Line 31: Line 31:
:Yes, we probably should add this to the article somehow...
:Yes, we probably should add this to the article somehow...
: [[User:Markhurd|Mark Hurd]] ([[User talk:Markhurd|talk]]) 01:16, 24 February 2009 (UTC)
: [[User:Markhurd|Mark Hurd]] ([[User talk:Markhurd|talk]]) 01:16, 24 February 2009 (UTC)



This article is wrong. The preprocessor does NOTHING to fix the dangling comma if you have no variadic arguments. GCC and VC++ have extensions to fix it, but the standard does not. You need at least one argument. The workaround is to just specify all arguments as variadic:

#define foo(...)

Revision as of 14:31, 11 February 2011

It should be said how the user (= body of the function) accesses to (possibly existing) extra arguments and their number. MFH: Talk 19:07, 12 May 2005 (UTC)[reply]


now you (Akihabara) added "no means is provided...", although I somehow remember some trick to find it out (but I stopped doing tricky things in C about 10 years ago...) - but maybe the tricks I used then are too tricky to be mentioned officially...(kindof calculations with stack pointers, return adresses sneaked from the stack, memory addresses of passed parameters, etc.etc.) - but wasn't there some simple (and official) VA_NUM_ARGS() macro or so? MFH: Talk 19:14, 13 May 2005 (UTC)[reply]

No. There may be compiler extensions, but there is no way for the macro itself to find out. Note this has nothing to do with stacks; you may be getting confused with variadic functions --Akihabara 22:49, 13 May 2005 (UTC)[reply]


The page says "Previous versions of Visual Studio lack this feature", but I'm compiling some variadic macros in VS 2003. --anon

more arguments

What happens if you use dprintf("Hello, world", "another string", foo, i);? That is: using a variadic macro with a different number of variables. That's what the variadic macros are good for, right? I don't see how this is supposed to work. --Abdull 09:24, 4 December 2007 (UTC)[reply]

The stdarg.h article describes how it works. . --Abdull 09:36, 4 December 2007 (UTC)[reply]

fewer arguments

It's worth mentioning the necessity to pass at least one argument to fill the '...' slot, distinct from variadic functions. In the variadic case C99 demands more arguments than identifiers in the definition (see ISO/EOC9899:TC3 6.10.3 constraint 4), but compilers often allow zero extra arguments. Even so, if __VA_ARGS__ is allowed to expand to an empty string then an expression like

#define dprintf(fmt, ...) realdprintf(__FILE__, __LINE__, fmt, __VA_ARGS__)
dprintf("Hello, world");

would expand to

realdprintf("example.c", 123, "Hello, world", );

where a trailing comma is left in the argument list. --212.44.20.129 (talk) 20:01, 11 February 2009 (UTC)[reply]

And the Microsoft C/C++ compiler caters for that case by dropping the trailing comma [1]
Yes, we probably should add this to the article somehow...
Mark Hurd (talk) 01:16, 24 February 2009 (UTC)[reply]


This article is wrong. The preprocessor does NOTHING to fix the dangling comma if you have no variadic arguments. GCC and VC++ have extensions to fix it, but the standard does not. You need at least one argument. The workaround is to just specify all arguments as variadic:

  1. define foo(...)