What does __VA_ARGS__ in a macro mean?
Asked Answered
F

3

64
/* Debugging */
#ifdef DEBUG_THRU_UART0
#   define DEBUG(...)  printString (__VA_ARGS__)
#else
void dummyFunc(void);
#   define DEBUG(...)  dummyFunc()   
#endif

I've seen this notation in different headers of C programming, I basically understood it's passing arguments, but I didn't understand what this "three dots notation" is called?

Can someone explain it with example or provide links also about VA Args?

Footworn answered 26/9, 2014 at 7:10 Comment(0)
G
65

It's a variadic macro. It means you can call it with any number of arguments. The three ... is similar to the same construct used in a variadic function in C

That means you can use the macro like this

DEBUG("foo", "bar", "baz");

Or with any number of arguments.

The __VA_ARGS__ refers back again to the variable arguments in the macro itself.

#define DEBUG(...)  printString (__VA_ARGS__)
               ^                     ^
               +-----<-refers to ----+

So DEBUG("foo", "bar", "baz"); would be replaced with printString ("foo", "bar", "baz")

Gracye answered 26/9, 2014 at 8:56 Comment(0)
I
19

The dots are called, together with the __VA_ARGS__, variadic macros

When the macro is invoked, all the tokens in its argument list [...], including any commas, become the variable argument. This sequence of tokens replaces the identifier __VA_ARGS__ in the macro body wherever it appears.

source, bold emphasis of mine.

A sample of usage:

#ifdef DEBUG_THRU_UART0
#   define DEBUG(...)  printString (__VA_ARGS__)
#else
void dummyFunc(void);
#   define DEBUG(...)  dummyFunc()   
#endif
DEBUG(1,2,3); //calls printString(1,2,3) or dummyFunc() depending on
              //-DDEBUG_THRU_UART0 compiler define was given or not, when compiling.
Interlocution answered 26/9, 2014 at 7:20 Comment(0)
P
0

Basically... it means that it converts the function into multiple arguments and you have the ability to add multiple arguments to your function. Of course, __va_args__ is used so that if you #define a variable or function, you can convert it into several arguments with... The point is that you should also use the va_list and va_start functions in addition to this mode that exists in #define.

#include<stdarg.h>
#include<stdio.h>

int sum(int, ...);

int main(void) {
    printf("Sum of 10, 20 and 30 = %d\n", sum(3, 10, 20, 30) );
    printf("Sum of 4, 20, 25 and 30 = %d\n", sum(4, 4, 20, 25, 30) );

    return 0;
}

int sum(int num_args, ...) {
    int val = 0;
    va_list ap;
    int i;

    va_start(ap, num_args);
    for(i = 0; i < num_args; i++) {
       val += va_arg(ap, int);
    }
    va_end(ap);
 
    return val;
}
Phototype answered 22/6, 2023 at 9:52 Comment(3)
Variadic macros (__VA_ARGS__) and variadic functions are not related at all.Patsis
Look at this link: snai.pe/posts/varargsPhototype
A good read and it certainly relates variadic macros to variadic functions, but it presents variadic macros only as a solution to improve variadic functions. You should not normally go from variadic macros to functions.Patsis

© 2022 - 2024 — McMap. All rights reserved.