/* 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?
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.
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.
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.
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.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With