Variadic macro in the C preprocessor

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 and will be added in C23. [2] [3]

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 ..., [4] 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. [5]

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] [6]

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

Support

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

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 [6] ) 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.

C23 solves this problem by introducing __VA_OPT__ like C++. [3]

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

The C preprocessor is the macro preprocessor for several computer programming languages, such as C, Objective-C, C++, and a variety of Fortran languages. The preprocessor provides inclusion of header files, macro expansions, conditional compilation, and line control.

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.

The printf family of functions in the C programming language are a set of functions that take a format string as input among a variable sized list of other values and produce as output a string that corresponds to the format specifier and given input values. 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. The design has been copied to expose similar functionality in other programming languages.

<span class="mw-page-title-main">C99</span> 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 assembly 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.

Harbour is a computer programming language, primarily used to create database/business programs. It is a modernized, open source and cross-platform version of the older Clipper system, which in turn developed from the dBase database market of the 1980s and 1990s.

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.

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, in the presence of two strong symbols by the same name, the linker resolves the symbol in favor of the first one found. This behavior allows an executable to override standard library functions, such as malloc(3). 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.

The Windows software trace preprocessor is a preprocessor that simplifies the use of WMI event tracing to implement efficient software tracing in drivers and applications that target Windows 2000 and later operating systems. WPP was created by Microsoft and is included in the Windows DDK. Although WPP is wide in its applicability, it is not included in the Windows SDK, and therefore is primarily used for drivers and driver support software produced by software vendors that purchase the Windows DDK.

<span class="mw-page-title-main">Vala (programming language)</span> Programming language

Vala is an object-oriented programming language with a self-hosting compiler that generates C code and uses the GObject system.

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. It is also the name of a Unix program for parsing command line arguments in shell scripts.

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.

X macros are a technique for reliable maintenance of parallel lists of code and/or data, whose corresponding items must be declared or executed in the same order. They are most useful where at least some of the lists cannot be composed by indexing, such as compile time.

Objective-C is a high-level general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Due to Apple macOS’s direct lineage from NeXTSTEP, Objective-C was the standard programming language used, supported, and promoted by Apple for developing macOS and iOS applications until the introduction of the Swift programming language in 2014.

C23 is the informal name for ISO/IEC 9899:2023, the next standard for the C programming language, which will replace C17. It was started in 2016 informally as C2x, and expected to be published in 2024. The most recent publicly available draft of C23 was released on April 1, 2023. The first WG14 meeting for the C2x draft was held in October 2019, virtual remote meetings were held in 2020 due to the COVID-19 pandemic, then various teleconference meetings continued to occur through 2023.

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". June 18, 2017. Retrieved December 24, 2022.
  3. 1 2 "WG14 - N3033 : Comma omission and comma deletion". 2022-07-20.
  4. 1 2 3 4 Variadic Macros – Using the GNU Compiler Collection (GCC)
  5. Laurent Deniau (2006-01-16). "__VA_NARG__". Newsgroup:  comp.std.c. Usenet:   dqgm2f$ije$1@sunnews.cern.ch.
  6. 1 2 3 Variadic Macros (C++)
  7. Recursive macros with C++20 __VA_OPT__
  8. 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
  9. Sun Studio feature comparison – http://developers.sun.com/sunstudio/support/CCcompare.html
  10. "C++2a Support in GCC" . Retrieved June 14, 2018.
  11. "C++ Support in Clang" . Retrieved June 14, 2018.
  12. "MSVC new preprocessor overview". September 10, 2020. Retrieved December 8, 2020.

See also