By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,427 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

whats this macro doing

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Affan Syed" <as***@usc.edu> wrote...
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
Jul 23 '05 #2

P: n/a
> #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
Jul 23 '05 #3

P: n/a
Victor Bazarov wrote:
"Affan Syed" <as***@usc.edu> wrote...
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.
Jul 23 '05 #4

P: n/a
Affan Syed wrote:
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.
Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.