58
/* 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?

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
ganeshredcobra
  • 1,881
  • 3
  • 28
  • 44

3 Answers3

61

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")

phuclv
  • 37,963
  • 15
  • 156
  • 475
nos
  • 223,662
  • 58
  • 417
  • 506
18

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.
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Theolodis
  • 4,977
  • 3
  • 34
  • 53
1

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;
}
cunknown
  • 29
  • 6