devxlogo

Macros with Variable Number of Arguments

Question:
Is there anyway to have a variable number of arguments passed to a macro? Yes, I know it can be done with inline, but this is for debugging purposes.

Answer:
Yes and no. Standard C and C++ don’t support variable number of arguments in a function-like macro. However, the newly approved C99 standard of the C programming language does support function-like macros with a variable number of arguments using the ellipsis (…) notation. The variable arguments (including the separating commas) are “collapsed” into a single extra argument that can be referenced as __VA_ARGS__ within the macro’s replacement list.

If you’re using a C99 compliant C compiler, you can use this feature. In C++, though, you should use superior techniques such as default parameters, templates, and function objects instead of macros.

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Seven Service Boundary Mistakes That Create Technical Debt

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.