Variadic macro

Last updated

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.

Contents

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. [1] Support for variadic macros with no arguments was added in C++20. [2]

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.

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. [3]

Both the C99 and C++11 standards require at least one argument, but since C++20 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. [4] [5]

Support

Several compilers support variable-argument macros when compiling C and C++ code: the GNU Compiler Collection 3.0, [4] Clang (all versions), [6] Visual Studio 2005, [5] C++Builder 2006, and Oracle Solaris Studio (formerly Sun Studio) Forte Developer 6 update 2 (C++ version 5.3). [7] 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, [8] Clang 6, [9] and Visual Studio 2019. [10]

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 functionvoidrealdbgprintf(constchar*SourceFilename,intSourceLineno,constchar*CFormatString,...);// Due to limitations of the variadic macro support in C++11 the following// straightforward solution can fail and should thus be avoided:////   #define dbgprintf(cformat, ...) \//     realdbgprintf (__FILE__, __LINE__, cformat, __VA_ARGS__)//// The reason is that////   dbgprintf("Hallo")//// gets expanded to////   realdbgprintf (__FILE__, __LINE__, "Hallo", )//// where the comma before the closing brace will result in a syntax error.//// GNU C++ supports a non-portable extension which solves this.////   #define dbgprintf(cformat, ...) \//     realdbgprintf (__FILE__, __LINE__, cformat, ##__VA_ARGS__)//// C++20 eventually supports the following syntax.////   #define dbgprintf(cformat, ...) \//     realdbgprintf (__FILE__, __LINE__, cformat __VA_OPT__(,) __VA_ARGS__)//// By using the 'cformat' string as part of the variadic arguments we can// circumvent the abovementioned incompatibilities.  This is tricky but// portable.#define dbgprintf(...) realdbgprintf (__FILE__, __LINE__, __VA_ARGS__)

dbgprintf() could then be called as

dbgprintf("Hello, world");

which expands to

realdbgprintf(__FILE__,__LINE__,"Hello, world");

Another example is

dbgprintf("%d + %d = %d",2,2,5);

which expands to

realdbgprintf(__FILE__,__LINE__,"%d + %d = %d",2,2,5);

Without variadic macros, writing wrappers to printf is not directly possible. The standard workaround is to use the stdargs functionality of C/C++, 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 [5] ) will silently eliminate the trailing comma. Other compilers (e.g.: GCC [4] ) support putting ## in front of __VA_ARGS__.

# define MYLOG(FormatLiteral, ...)  fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, __VA_ARGS__)

The following application works

MYLOG("Too many balloons %u",42);

which expands to

fprintf(stderr,"%s(%u): ""Too many balloons %u""\n",__FILE__,__LINE__,42);

which is equivalent to

fprintf(stderr,"%s(%u): Too many balloons %u\n",__FILE__,__LINE__,42);

But look at this application:

MYLOG("Attention!");

which expands to

fprintf(stderr,"%s(%u): ""Attention!""\n",__FILE__,__LINE__,);

which generates a syntax error with GCC.

GCC supports the following (non-portable) extension:

# define MYLOG(FormatLiteral, ...)  fprintf (stderr, "%s(%u): " FormatLiteral "\n", __FILE__, __LINE__, ##__VA_ARGS__)

which removes the trailing comma when __VA_ARGS__ is empty.

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:

# define dbgprintf(x) realdbgprintf x

dbgprintf() could then be called as:

dbgprintf(("Hello, world %d",27));

which expands to:

realdbgprintf("Hello, world %d",27);

Related Research Articles

C (programming language) general-purpose programming language

C is a general-purpose, procedural computer programming language supporting structured programming, lexical variable scope, and recursion, with a static type system. By design, C provides constructs that map efficiently to typical machine instructions. It has found lasting use in applications previously coded in assembly language. Such applications include operating systems and various application software for computer architectures that range from supercomputers to PLCs and embedded systems.

In software development, Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called Makefiles which specify how to derive the target program. Though integrated development environments and language-specific compiler features can also be used to manage a build process, Make remains widely used, especially in Unix and Unix-like operating systems.

The C preprocessor or cpp is the macro preprocessor for the C, Objective-C and C++ computer programming languages. The preprocessor provides the ability for the inclusion of header files, macro expansions, conditional compilation, and line control.

In the C and C++ programming languages, an inline function is one qualified with the keyword inline; this serves two purposes. Firstly, it serves as a compiler directive that suggests that the compiler substitute the body of the function inline by performing inline expansion, i.e. by inserting the function code at the address of each function call, thereby saving the overhead of a function call. In this respect it is analogous to the register storage class specifier, which similarly provides an optimization hint. The second purpose of inline is to change linkage behavior; the details of this are complicated. This is necessary due to the C/C++ separate compilation + linkage model, specifically because the definition (body) of the function must be duplicated in all translation units where it is used, to allow inlining during compiling, which, if the function has external linkage, causes a collision during linking. C and C++ resolve this in different ways.

The syntax of the C programming language is the set of rules governing writing of software in the C language. It is designed to allow for programs that are extremely terse, have a close relationship with the resulting object code, and yet provide relatively high-level data abstraction. C was the first widely successful high-level language for portable operating-system development.

printf format string refers to a control parameter used by a class of functions in the input/output libraries of C and many other programming languages. The string is written in a simple template language: characters are usually copied literally into the function's output, but format specifiers, which start with a % character, indicate the location and method to translate a piece of data to characters.

C99 C programming language standard, 1999 revision

C99 is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version (C90) with new features for the language and the standard library, and helps implementations make better use of available computer hardware, such as IEEE 754-1985 floating-point arithmetic, and compiler technology. The C11 version of the C programming language standard, published in 2011, replaces C99.

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

In mathematics and in computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages.

In the C and C++ programming languages, pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. Thus, #pragma once serves the same purpose as include guards, but with several advantages, including: less code, avoidance of name clashes, and sometimes improvement in compilation speed. On the other hand, #pragma once is not necessarily available in all compilers and its implementation is tricky and might not always be reliable.

sizeof is a unary operator in the programming languages C and C++. It generates the storage size of an expression or a data type, measured in the number of char-sized units. Consequently, the construct sizeof (char) is guaranteed to be 1. The actual number of bits of type char is specified by the preprocessor macro CHAR_BIT, defined in the standard include file limits.h. On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t.

assert.h is a header file in the standard library of the C programming language that defines the C preprocessor macro assert . In C++ it is also available through the <cassert> header file.

In computer programming, an anonymous function is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions, or used for constructing the result of a higher-order function that needs to return a function. If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. In contrast, two strong symbols that share a name yield a link error during link-time. When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, a declared strong symbol without a definition triggers an undefined symbol link error.

stdarg.h is a header in the C standard library of the C programming language that allows functions to accept an indefinite number of arguments. It provides facilities for stepping through a list of function arguments of unknown number and type. C++ provides this functionality in the header cstdarg.

In computer programming, variadic templates are templates that take a variable number of arguments.

Blocks are a non-standard extension added by Apple Inc. to Clang's implementations of the C, C++, and Objective-C programming languages that uses a lambda expression-like syntax to create closures within these languages. Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+, although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+ and non-Apple systems.

getopt is a C library function used to parse command-line options of the Unix/POSIX style. It is a part of the POSIX specification, and is universal to Unix-like systems.

In computer programming, ellipsis notation is used to denote ranges, an unspecified number of arguments, or a parent directory. Most programming languages require the ellipsis to be written as a series of periods; a single (Unicode) ellipsis character cannot be used.

Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. It was the main programming language supported by Apple for macOS, iOS, and their respective application programming interfaces (APIs), Cocoa and Cocoa Touch, until the introduction of Swift in 2014.

References

  1. Working draft changes for C99 preprocessor synchronization – http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1653.htm
  2. "Comma omission and comma deletion". July 12, 2017. Retrieved June 14, 2018.
  3. Laurent Deniau (2006-01-16). "__VA_NARG__". Newsgroup:  comp.std.c. Usenet:   dqgm2f$ije$1@sunnews.cern.ch.
  4. 1 2 3 Variadic Macros – Using the GNU Compiler Collection (GCC)
  5. 1 2 3 Variadic Macros (C++)
  6. Clang source code change that mentions __VA_ARGS__ support (2006-07-29), note that Clang was open-sourced in 2007. http://llvm.org/viewvc/llvm-project?view=revision&revision=38770
  7. Sun Studio feature comparison – http://developers.sun.com/sunstudio/support/CCcompare.html
  8. "C++2a Support in GCC" . Retrieved June 14, 2018.
  9. "C++ Support in Clang" . Retrieved June 14, 2018.
  10. "MSVC new preprocessor overview". September 10, 2020. Retrieved December 8, 2020.

See also