Questions tagged [variadic-macros]

Variadic macros are a feature of the C-preprocessor that permit a variable number of arguments to a macro. They were added in the 1999 revision of the C standard.

Variadic macros are a feature of the C-preprocessor that permit a variable number of arguments to a macro. They were added in the 1999 revision of the C standard.

More details can be obtained from:

295 questions
13
votes
1 answer

A #define in C with three dots

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__)) This is definition for these 2 macros; later in the code…
Ken
  • 2,105
  • 3
  • 19
  • 22
12
votes
2 answers

C Preprocessor to split "int x" into int & x

I need to be able to get the following: #define MY_MACRO(PARAM1,PARAM2) \ MY_OTHER_MACRO(TYPENAME_OF(PARAM1),PARAMNAME_OF(PARAM1));\ MY_OTHER_MACRO(TYPENAME_OF(PARAM2),PARAMNAME_OF(PARAM2));\ to cause MY_MACRO(int x,char *z) to compile…
unsynchronized
  • 4,828
  • 2
  • 31
  • 43
12
votes
3 answers

Error when defining a stringising macro with __VA_ARGS__

I have been trying to implement a function macro in C that prepends "DEBUG: ", to the argument, and passes its arguments to printf: #define DBG(format, ...) printf("DEBUG: " #format "\n", __VA_ARGS__) This gives me this error in…
11
votes
3 answers

Variadic macro trick

What's the trick to create a variadic macro FOO(a1, a2, a3,..., an) such that it expands to FOOn(a1, a2, a3,..., an) for values of n in whatever preselected bounded range you choose? That is, FOO(a) should expand to FOO1(a), FOO(a, b, c) to FOO3(a,…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
11
votes
2 answers

'ISO C99 requires at least one argument for the "..." in a variadic macro' When specifically using c11 and -Wno-variadic-macros

I have a simple: #define log(text, ...) fprintf(stderr, "stuff before" text "stuff after", ## __VA_ARGS__); which is triggering: error: ISO C99 requires at least one argument for the "..." in a variadic macro [-Werror] Should using -std=c11 and…
Ray C
  • 547
  • 2
  • 7
  • 24
11
votes
3 answers

Variadic macros with zero arguments, and commas

Consider this macro: #define MAKE_TEMPLATE(...) template When used with zero arguments it produces bad code since the compiler expects an identifier after the comma. Actually, VC's preprocessor is smart enough to remove…
uj2
  • 2,255
  • 2
  • 21
  • 32
11
votes
1 answer

Variadic macro without arguments

I am using some logging macros, which are supposed to print out the information provided by the __PRETTY_FUNCTION__ macro and if needed name and value of up to two arguments. A simplified version of my code looks like template
Jan Hajer
  • 232
  • 2
  • 14
11
votes
1 answer

C++ preprocessor removes whitespace in calls to variadic macros (Solaris Studio 12.3)

The C++ preprocessor of Oracle Solaris Studio 12.3 removes whitespace completely when expanding __VA_ARGS__. Can anybody confirm this behaviour on their system? Is it a known compiler bug? Are there any workarounds for this problem? To illustrate,…
Claudio
  • 3,089
  • 2
  • 18
  • 22
10
votes
1 answer

How to single out the first parameter sent to a macro taking only a variadic parameter

I try to get at the first actual parameter sent to a variadic macro. This is what I tried, and which does not work in VS2010: #define FIRST_ARG(N, ...) N #define MY_MACRO(...) decltype(FIRST_ARG(__VA_ARGS__)) When I look at the preprocessor output…
Bengt Gustafsson
  • 517
  • 4
  • 10
9
votes
2 answers

Variadic macro and trailing comma

I am trying to do object-orientation in C and want to have a syntactic sugar macro for the notation object->vtable->method(object, arg1, arg2) into send(object, method, arg1, arg2) Unfortunately when a method takes no argument, the trailing comma…
anotherCode245
  • 703
  • 1
  • 8
  • 20
8
votes
1 answer

How do I use variadic arguments with printf in a macro?

I have found no way to merge the first printf into the second: unsigned get_time_now(void) {return 1;} #define DEBUG_PRINT 1 #define debug_tcprintf(fmt, ...) do { \ if (DEBUG_PRINT) { \ unsigned p_time_now =…
Øyvind Teig
  • 139
  • 1
  • 9
8
votes
2 answers

Variadic function calling a variadic macro

I have an inline variadic function inline int foo(...) I need foo() to call a macro (let's call it MACRO), which is also variadic. Basically I need foo() to pass all its input parameters to MACRO. Redefining foo() as another macro would be an easy…
lulijeta
  • 900
  • 1
  • 9
  • 19
8
votes
2 answers

Variadic macros alternative in ANSI C

I know that variadic macros have been added in C99 (and via GNU extensions). I've been wondering if there is a nice alternative in ANSI C. I've come up with something like this, but it's still kind of awkward: void log_info(const char *file, int…
Alex
  • 948
  • 1
  • 13
  • 17
7
votes
2 answers

C Function that calculates total sizeof arguments

I'm currently looking to calculate the total size of arguments passed into a function, in bytes. In theory, one can just write out sizeof(x) for every argument. However, this is a tremendous waste of time if one wants to do this for a lot of…
Namey
  • 1,172
  • 10
  • 28
7
votes
2 answers

Can I define variadic C preprocessor macros with __VA_ARGS in the middle instead of the end?

GCC complains if I do this: #define M(obj,met, ..., contents) obj##_##met(const void * self, __VA_ARGS__) { \ contents \ } Giving me these 2 reasons: error: missing ')' in macro parameter list warning: __VA_ARGS__ can only appear in the…
salvador p
  • 2,905
  • 5
  • 26
  • 26
1
2
3
19 20