whats this macro doing

A

Affan Syed

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.
 
V

Victor Bazarov

Affan Syed said:
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)

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
 
J

John Smith

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

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
 
R

Ron Natalie

Affan said:
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.
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.
 
R

Ron Natalie

Victor said:
Affan Syed said:
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)


You will have to ask in comp.lang.c . Macros with undefined number
of arguments (...) are not legal in C++.

It doesn't even look right for standard C's vararg'd macros and ##
operator.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,045
Latest member
DRCM

Latest Threads

Top