Thread: What's the __VA_ARGS__ macro and how do I use it?

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    228

    What's the __VA_ARGS__ macro and how do I use it?

    Hey.
    I'm trying to wrap my head around the idea of variadic macros, and I have several questions:

    1. Is this a C++ thing, or does is also exist in C?
    2. How do I use it?
    3. What's the __VA_ARGS__ macro has to do with it?

    It all started when I found this. In the link you can find "A Lightwight Logger For C++".
    I implemented it in a similar way to the suggested way in the link, but when I came to use it the same way they did (i.e. with the macro definitions LOG(...)/LOG_ERR(...) etc...), I got lost, because I did not fully understand how variadic macros work.
    I tried to look around online about variadic macro and found many people use this weird __VA_ARGS__, but none bother to explain what on earth is this macro and what it does.

    Notice that I am not talking about variadic template, but rather variadic macros, and while these two (might, idk) have similarities, I managed to understand (and use) the variadic template for implementing the logger in the above link.

    Thanks.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Absurd View Post
    1. Is this a C++ thing, or does is also exist in C?
    It is a C thing, and is also supported in C++. In C++, we now have variadic templates, which are safer and preferable to variadic macros and C-style variadic functions.

    Quote Originally Posted by Absurd View Post
    2. How do I use it?
    It's most often used with C functions which take variable arguments. The GCC Manual has a description of what to do with it.

    Quote Originally Posted by Absurd View Post
    3. What's the __VA_ARGS__ macro has to do with it?
    The link I shared explains that as well.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,643
    __VA_ARGS__ is not a macro but a special identifier used to show where to put the variable arguments in the expansion of a variadic macro. It's a C99 thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. macro use.
    By gourav87 in forum C Programming
    Replies: 8
    Last Post: 02-15-2012, 11:04 AM
  2. __VA_ARGS__ with empty argument.
    By xinwu in forum C Programming
    Replies: 4
    Last Post: 11-09-2010, 03:02 AM
  3. Use of Macro
    By NKP in forum C Programming
    Replies: 5
    Last Post: 06-02-2010, 08:30 PM
  4. __va_args__
    By syzygy in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2010, 11:33 AM
  5. macro
    By sballew in forum C Programming
    Replies: 2
    Last Post: 10-10-2001, 07:51 PM