Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does __VA_ARGS__ in a macro mean?

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

like image 722
ganeshredcobra Avatar asked Sep 26 '14 07:09

ganeshredcobra


People also ask

What type is __ Va_args __?

To use variadic macros, the ellipsis may be specified as the final formal argument in a macro definition, and the replacement identifier __VA_ARGS__ may be used in the definition to insert the extra arguments. __VA_ARGS__ is replaced by all of the arguments that match the ellipsis, including commas between them.

Where is __ Va_args __ defined?

The C standard mandates that the only place the identifier __VA_ARGS__ can appear is in the replacement list of a variadic macro. It may not be used as a macro name, macro argument name, or within a different type of macro. It may also be forbidden in open text; the standard is ambiguous.

What is #__ Va_args __?

16.3.An identifier __VA_ARGS__ that occurs in the replacement list shall be treated as if it were a parameter, and the variable arguments shall form the preprocessing tokens used to replace it. this is the same for varags as it is for normal parameters.

What is __ FILE __ in C?

The __FILE__ macro expands to a string whose contents are the filename, surrounded by double quotation marks ( " " ). If you change the line number and filename, the compiler ignores the previous values and continues processing with the new values. The #line directive is typically used by program generators.


1 Answers

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

like image 112
nos Avatar answered Sep 20 '22 10:09

nos