ArticleslgStudy

science

Variadic macro

Variadic macro is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Variadic macro rather than just read about it. In short: A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments. Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard.

Key takeaways

  • Variadic macro belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Variadic macro to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Variadic macro from memory before moving on to harder problems.

Reference excerpt

A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments. Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2011 in ISO/IEC 14882:2011 (C++11) revision of the C++ language standard. Support for variadic macros with no arguments was added in C++20 and C23.

C/C++

Declaration syntax The declaration syntax is similar to that of variadic functions: a sequence of three full stops "..." is used to indicate that one or more arguments must be passed. During macro expansion each occurrence of the special identifier __VA_ARGS__ in the macro replacement list is replaced by the passed arguments. Additionally, regular macro arguments may be listed before the ..., but regular arguments may not be listed after the .... No means is provided to access individual arguments in the variable argument list, nor to find out how many were passed. However, macros can be written to count the number of arguments that have been passed. Both the C99 and C++11 standards require at least one argument, but since C++20 and C23 this limitation has been lifted through the __VA_OPT__ functional macro. The __VA_OPT__ macro is replaced by its argument when arguments are present, and omitted otherwise. Common compilers also permit passing zero arguments before this addition, however. The C preprocessor rules prevent macro names in the argument of __VA_OPT__ from expanding recursively. It is possible to work around this limitation up to an arbitrary fixed number of recursive expansions, however.

Support Several compilers support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection 3.0, Clang (all versions), Visual Studio 2005, C++Builder 2006, and Oracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3). GCC also supports such macros when compiling Objective-C. Support for the __VA_OPT__ macro to support zero arguments has been added in GNU Compiler Collection 8, Clang 6, and Visual Studio 2019.

Example If a printf-like function dbgprintf() were desired, which would take the file and line number from which it was called as arguments, the following solution applies. Our implemented function:

Due to limitations of the variadic macro support in C++11 the following straightforward solution can fail and should thus be avoided:

The reason is that

gets expanded to

where the comma before the closing brace will result in a syntax error. GNU C++ supports a non-portable extension which solves this:

C++20 supports the following syntax.

By using the cformat string as part of the variadic arguments we can circumvent the abovementioned incompatibilities. This is tricky but portable.

dbgprintf() could then be called as

which expands to

Another example is:

which expands to

Without variadic macros, writing wrappers to printf is not directly possible. The standard workaround is to use the <stdarg.h> header, and have the function call vprintf instead.

Trailing comma There is a portability issue with generating a trailing comma with empty args for variadic macros in C99. Some compilers (e.g., Visual Studio when not using the new standard-conformant preprocessor) will silently eliminate the trailing comma. Other compilers (e.g.: GCC) support putting ## in front of __VA_ARGS__.

The following application works

which expands to

which is equivalent to

But look at this application:

which expands to

which generates a syntax error with GCC. GCC supports the following (non-portable) extension:

which removes the trailing comma when __VA_ARGS__ is empty. C23 solves this problem by introducing __VA_OPT__ like C++.

Alternatives Before the existence of variable-arguments in C99, it was quite common to use doubly nested parentheses to exploit the variable number of arguments that could be supplied to the printf() function:

dbgprintf() could then be called as:

which expands to:

Rust

Rust's macros are syntactic procedural macros, which expand over the abstract syntax tree and are hygenic, unlike C's macros, making them more powerful for code generation. In Rust, a variadic interface (also known as a variadic macro), allows for taking an arbitrary number of arguments. Rust lacks variadic functions and variadic templates, thus the only way variadic parameters can be accomplished is through said variadic interfaces, which use repetition patterns through macro_rules!. For example, in $( ... ),*, $() denotes a repeating pattern, , denotes a separator, * denotes zero or more repetitions (a Kleene star, while + which matches one or more would be a Kleene plus). One such example of implementing a variadic macro is the following, equivalent to vec!:

References

See also Variadic function Variadic template

Worked examples

Example 1 — a first encounter with Variadic macro

Start with the simplest possible case. Write down what Variadic macro claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Variadic macro before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Variadic macro ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Variadic macro

In research
Variadic macro appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Variadic macro in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Variadic macro is common in secondary-school and first-year university syllabi. It links to neighbouring topics C++, C (programming language), Rust (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Variadic macro outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Variadic macro” →

How to study Variadic macro in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Variadic macro means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Variadic macro out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Variadic macro in simple terms?

A variadic macro is a feature of some computer programming languages, especially the C preprocessor, whereby a macro may be declared to accept a varying number of arguments. Variable-argument macros were introduced in 1999 in the ISO/IEC 9899:1999 (C99) revision of the C language standard, and in 2…

Why does Variadic macro matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Variadic macro?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Variadic macro.

Tags

  • C++
  • C (programming language)
  • Rust (programming language)

Keep exploring