Thread: __VA_ARGS__ with empty argument.

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    __VA_ARGS__ with empty argument.

    Hi, everyone! I defined a macro like this:

    Code:
    #define POUT(format, ...) fprintf(stdout, format, __VA_ARGS__)
    But at least one argument must follow the 'format' string. I want to use __VA_ARGS__ with zero argument. How can I do that?

    I know I can do that with GNU extension. But I'm wondering a more portable solution.

    GNU extension.

    Code:
    #define POUT(format, ...) fprintf(stdout, format, ##__VA_ARGS__)
    Thanks in advance!
    Last edited by xinwu; 11-08-2010 at 05:39 AM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I'm not sure, but if there's a GNU extensions then I doubt you can do that. Maybe it's better to make it a function rather than a macro, and use vfprintf. Of course, a hack-solution would be:
    Code:
    #define POUT(...) fprintf(stdout, __VA_ARGS__)
    But that won't work in all cases.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    I prefer macro because I want to avoid unnecessary overhead due to function calls.

    My actually definition of the macro in the program is.

    Code:
    #define ERROR (fmt, ...) {\
      fprintf(stderr, "Error [%s %d]:" fmt, __func__, __LINE__, __VA_ARGS__);\
      exit (1); \
    }
    Because 'fmt' and '__VA_ARGS__' are separated, I think GNU extension might be the best. However, I find that only gcc and icc follow this extension. Compilers from Sun have their own extension, which is different from GNU.
    Last edited by xinwu; 11-08-2010 at 11:30 AM.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by xinwu View Post
    I prefer macro because I want to avoid unnecessary overhead due to function calls.

    My actually definition of the macro in the program is.

    Code:
    #define ERROR (fmt, ...) {\
      fprintf(stderr, "Error [%s %d]:", fmt, __func__, __LINE__, __VA_ARGS__);\
      exit (1); \
    }
    Because 'fmt' and '__VA_ARGS__' are separated, I think GNU extension might be the best. However, I find that only gcc and icc follow this extension. Compilers from Sun have their own extension, which is different from GNU.
    That's premature optimization, really. A function call might actually be faster than a macro. You won't know until you tested both in exactly the finished program. Also, a function may be declared inline, in which case it would be pretty much the same as the macro. But you shouldn't inline stuff unless you're 100% sure it's faster - and honestly, I'm not convinced it is, and I highly doubt you know for sure.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not only is it premature optimisation, it is completely unnecessary optimisation

    1) The macro call expands to two function calls anyway, unless one or both of those functions happen to be inlined by the compiler.

    2) The second of those function calls is exit() which means, at most, the expansion of that macro can only ever be invoked once at run time.

    Code that prints output and then exits the program is not exactly an outstanding candidate for optimisation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 06:02 AM
  2. Help with binary file c++
    By lucky_mutani in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:24 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM