Thread: macros with unspecified number of arguments

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question macros with unspecified number of arguments

    How do you define a macro with an unspecified number of arguments? For example, if you want to define a macro called DBG with at least one argument, that calls printf with all arguments and then prints out a new line at the end, how can you do that?

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    By using a function instead and using vprintf

  3. #3
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Thumbs down

    Quote Originally Posted by Thantos
    By using a function instead and using vprintf
    What!? vprintf? That wasn't what I said! Ok, I want to write a macro, you can take a look again at the top statement.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I know what you said. Just because you want something doesn't mean its a good idea.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or even that it can be done. C isn't about what you want. Unless of course the language happens to agree with you.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by quzah
    Or even that it can be done. C isn't about what you want. Unless of course the language happens to agree with you.


    Quzah.
    I know it can be done! I have seen just such a macro, but I didn't quite understand the code, I think it was using some defined thing I didn't know what it was then, when it called printf, it used the defined thing instead of the "optional" arguments. And the macro definition was "#define DBG(x, ...) " and then something. Isn't there anyone who knows how to do?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://c-faq.com/cpp/varargs.html

    [edit]It looks like someone followed the link!
    gcc has an extension which allows a function-like macro to accept a variable number of arguments, but it's not standard.
    Last edited by Dave_Sinkula; 07-21-2006 at 08:11 PM. Reason: ...
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    If you are using GCC, you can have variadic macros as an extension. If this is applicable to you, check the docs for more details.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TriKri
    I know it can be done! I have seen just such a macro
    I've seen fflush( stdin ) and void main too. What was your point again?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem
    While it's true variable argument lists of macros are now supported in C99, the link is wrong on one count. Very few C++ compilers provide this feature (as an extension).

    In C++ and C89, if you want something with variable argument lists, you need to use variable argument functions.

    If the usage you intend is essentially a wrapper for a variable argument function, it is possible to do a cludge in both C89 and C++. For example;
    Code:
    #define PRINT(x) fprintf(x)
    
    int main()
    {
         PRINT((stdout, "Hello %s\n", "world"));    /* note use of two sets of () on macro use */
    }
    There are catches with this. The first is that it is "all or nothing" --- to illustrate, in this example, it is not possible to change the PRINT macro so it specifies the first argument to fprintf() but allowing the remaining lis of arguments provided to printf() to be variable. The need for using two sets of brackets, at the point where the macro is used, is a gotcha for whoever uses the macro --- if they forget the two sets, the result is typically a lot of confusing error messages from the compiler.

  12. #12
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by quzah
    I've seen fflush( stdin ) and void main too. What was your point again?


    Quzah.
    Do you say it's a bad idea or what!?

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by TriKri
    Do you say it's a bad idea or what!?
    Are you trolling? Those answers are in the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    Question

    Quote Originally Posted by Dave_Sinkula
    Are you trolling? Those answers are in the FAQ.
    Trolling what? Isn't that the same as fishing? FAQ's is allways so hard to search when looking for such a specific thing as "macros with an unspecified number of arguments".

  15. #15
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by quzah
    I've seen fflush( stdin ) and void main too. What was your point again?


    Quzah.
    Just another question, why shouldn't I use fflush(stdin) or what should I use instead? And I'm sorry quzah if I have been rude to you, I didn't mean to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Making a program take a variable number of arguments
    By subtled in forum C Programming
    Replies: 1
    Last Post: 04-17-2007, 05:38 AM
  4. Perfect number...
    By Argo_Jeude in forum C++ Programming
    Replies: 8
    Last Post: 07-12-2005, 01:53 PM
  5. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 01:10 PM