whats this macro doing

Discussion in 'C++' started by Affan Syed, Feb 20, 2005.

  1. Affan Syed

    Affan Syed Guest

    Can any body give me an idea what the heck is happening over here :)


    #define UARTOutput(__x,__y,__args...) do { \
    static char __s[] PROGMEM = __y; \
    if (debug_out) \
    printf_P(__s, ## __args); \
    } while (0)

    Thankyou.
     
    Affan Syed, Feb 20, 2005
    #1
    1. Advertisements

  2. You will have to ask in comp.lang.c . Macros with undefined number
    of arguments (...) are not legal in C++. Also, this is certainly
    something from the implementation or the library, since normal users
    are prohibited from using double underscores for identifiers.

    V
     
    Victor Bazarov, Feb 20, 2005
    #2
    1. Advertisements

  3. Affan Syed

    John Smith Guest

    #define UARTOutput(__x,__y,__args...) do { \
    There are a number of undefined things in the expression but the bottom line
    is:

    if (debug_out)
    printf_P(...);

    If debug_out is set then printf alike function is called. The code is
    wrapped into a do { ... } while (0) loop. Essentially it's a while loop
    which only takes 1 round and will go out and continue after while(0) right
    away.
    I assume this is done to allow limited scope creation of the variable static
    char __s[].

    Hope this helps.

    -- John
     
    John Smith, Feb 21, 2005
    #3
  4. Affan Syed

    Ron Natalie Guest

    It looks like it's a printf like macro. UARTOutput( unused, "%s\n", "str");

    It however, isn't C++ (or for that matter standard C). C++ doesn't have the
    variable arg construct, and this doesn't look like it's using the C facility
    for doing such correctly either.

    The do { ... } while(0) construct is a macro writers hack to make the multiple
    statements look like a single function without causing grief to things that can
    only eat a single statement. For example, a simpler example:

    #define F(x) ++x; printf("%d\n", x)

    would perform wierdly in this sort of instance:

    if(condition) F(x) ;

    If the condition were false, the ++x would not be executed, but the printf would be.
    If you wrap the do...while around it it then behaves more as expected.
     
    Ron Natalie, Feb 21, 2005
    #4
  5. Affan Syed

    Ron Natalie Guest

    It doesn't even look right for standard C's vararg'd macros and ##
    operator.
     
    Ron Natalie, Feb 21, 2005
    #5
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.