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

use of do{...}while(0)

P: n/a
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?
--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 14 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?


It's a clever way to make a multi-statement macro appear to work like a
function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition
to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"I wish someone we knew would die so we could leave them flowers."
- A 6-year-old girl, upon seeing flowers in a cemetery
Nov 14 '05 #2

P: n/a
sathyashrayan wrote:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?


The answer text at 10.4 is probably about as good an explanation as is needed
if you understand the context. It's about writing a C language macro that
expands effectively into several standalone C statements.

Just mentally think about what the do{...}while(0) wrapper is doing and you'll
see how the "trick" works.

- Larry Weiss
Nov 14 '05 #3

P: n/a
On Wed, 26 Jan 2005 15:16:16 +0000, Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?
It's a clever way to make a multi-statement macro appear to work like a
function.


Not a function as such, a single statement requiring a ; at the end to
complete it.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);} if (willDoMagic)
DO_MAGIC_STUFF(quux);
You're right though that it is very likely to appear in a function-like
macro.
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).


And there's a big clue in my reply. :-)

Lawrence

Nov 14 '05 #4

P: n/a
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?

It's a clever way to make a multi-statement macro appear to work like a
function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition
to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).


I have got zero marks in both of the exercise!?

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 14 '05 #5

P: n/a
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?

It's a clever way to make a multi-statement macro appear to work like a
function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition
to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).


I have got zero marks in both of the exercises!?

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 14 '05 #6

P: n/a
sathyashrayan <lo****************@nomail.com> scribbled the following:
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?
It's a clever way to make a multi-statement macro appear to work like a
function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition
to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).

I have got zero marks in both of the exercises!?


Let's preprocess the code by hand. The first version will expand to:

if (willDoMagic)
{foo(quux); bar(quux);};
else
printf("Won't do magic stuff!\n");

The second will expand to:

if (willDoMagic)
do {foo(quux); bar(quux);} while (0);
else
printf("Won't do magic stuff!\n");

Now take a good look at these, particularly the "if" branch in the first
example.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Last year he disrespected me - and then he showed lack of respect."
- Anthony Mason
Nov 14 '05 #7

P: n/a
sathyashrayan wrote:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?


#define TEST(_x) \
{ \
/* do something
* with _x
*/ \
}

works too.

Regards,
Jonathan.

--
"Women should come with documentation." - Dave
Nov 14 '05 #8

P: n/a
Jonathan Burd <jo***********@removemegmail.com> scribbled the following:
sathyashrayan wrote:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?
#define TEST(_x) \
{ \
/* do something
* with _x
*/ \
} works too.


No it doesn't. I just explained why.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"Last year he disrespected me - and then he showed lack of respect."
- Anthony Mason
Nov 14 '05 #9

P: n/a
Joona I Palaste wrote:
Jonathan Burd <jo***********@removemegmail.com> scribbled the following:
sathyashrayan wrote:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?


#define TEST(_x) \
{ \
/* do something
* with _x
*/ \
}


works too.

No it doesn't. I just explained why.


Agreed.

Regards,
Jonathan.

--
"Women should come with documentation." - Dave
Nov 14 '05 #10

P: n/a


Jonathan Burd wrote:
sathyashrayan wrote:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?

#define TEST(_x) \
{ \
/* do something
* with _x
*/ \
}

works too.


In most but not all cases.
Just have a look at FAQ 10.4 or the other posts in this thread.

-Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #11

P: n/a
Michael Mair wrote:
#define TEST(_x) \
{ \
/* do something
* with _x
*/ \
}

works too.

In most but not all cases.
Just have a look at FAQ 10.4 or the other posts in this thread.

-Michael


It will work fine as long as you don't put a semi-colon while calling
the macro, i.e. TEST(abc) will work but TEST(abc); won't. Macros are
expanded before checking for syntax errors, so TEST(abc) will not throw
up any syntax errors, but it will appear confusing to someone reading
the code.

Vimal.
Nov 14 '05 #12

P: n/a
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:

What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?

It's a clever way to make a multi-statement macro appear to work like a
function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition
to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).

I have got zero marks in both of the exercises!?

Let's preprocess the code by hand. The first version will expand to:

if (willDoMagic)
{foo(quux); bar(quux);};
else
printf("Won't do magic stuff!\n");

The functions will called till willDoMAgic becomes false.
The second will expand to:

if (willDoMagic)
do {foo(quux); bar(quux);} while (0);
else
printf("Won't do magic stuff!\n");
Both functions are called , as like above, only once since the value
in the while is zero.
Now take a good look at these, particularly the "if" branch in the first
example.


But I don't find any difference in the two versions of macros since
both function is called only once.

BTW, TELL THE ANSWER PLEASE.........

--
"combination is the heart of chess"

A.Alekhine

Mail to:
sathyashrayan AT gmail DOT com

Nov 14 '05 #13

P: n/a

Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following: What is the use of the above do-while. Is it simply a style issue.Since the above same condition will applied with out a do-while(0);because the loop executes only once. I went through the faq section 10.4but I am little understood. Any answers please?

It's a clever way to make a multi-statement macro appear to work like a function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).

I have got zero marks in both of the exercises!?


Let's preprocess the code by hand. The first version will expand to:

if (willDoMagic)
{foo(quux); bar(quux);};
else
printf("Won't do magic stuff!\n");

The second will expand to:

if (willDoMagic)
do {foo(quux); bar(quux);} while (0);
else
printf("Won't do magic stuff!\n");

Now take a good look at these, particularly the "if" branch in the

first example.
I cannot find any difference in branch "if", I want to get a answer,
too.
Thank you. --
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\ \-------------------------------------------------------- rules! --------/ "Last year he disrespected me - and then he showed lack of respect."
- Anthony Mason


Nov 14 '05 #14

P: n/a
sathyashrayan wrote:

Joona I Palaste wrote:

.... snip ...

Let's preprocess the code by hand. The first version will expand to:

if (willDoMagic)
{foo(quux); bar(quux);};
else
printf("Won't do magic stuff!\n");

The functions will called till willDoMAgic becomes false.
The second will expand to:

if (willDoMagic)
do {foo(quux); bar(quux);} while (0);
else
printf("Won't do magic stuff!\n");

Both functions are called , as like above, only once since the value
in the while is zero.
Now take a good look at these, particularly the "if" branch in the
first example.


But I don't find any difference in the two versions of macros since
both function is called only once.

BTW, TELL THE ANSWER PLEASE.........


He did. It's right in front of your nose.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #15

P: n/a
Zilla wrote:
Joona I Palaste wrote:
<snip>
I cannot find any difference in branch "if", I want to get a answer,
too.
Thank you.


<snip>

What do you think this does?
----------------------------

if (condition)
{
/* do something here */
}; /* notice the semicolon. */
else
{
/* do something else here. */
}

Now try this:
-------------

if (condition)
do
{
/* do something here */
} while(0);
else
{
/* do something else here. */
}

Regards,
Jonathan.

--
"Women should come with documentation." - Dave
Nov 14 '05 #16

P: n/a
Joona I Palaste wrote:
Jonathan Burd <jo***********@removemegmail.com> scribbled the following:
sathyashrayan wrote:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section 10.4
but I am little understood. Any answers please?


#define TEST(_x) \
{ \
/* do something
* with _x
*/ \
}


works too.

No it doesn't. I just explained why.


I figured why I didn't come across the problem you have shown before.
I prefer using braces for if-else ladders even if they have only
one statement each. Anyhow, thanks for pointing out.

Regards,
Jonathan.

--
"Women should come with documentation." - Dave
Nov 14 '05 #17

P: n/a

Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue. Since the above same condition will applied with out a do-while(0); because the loop executes only once. I went through the faq section 10.4 but I am little understood. Any answers please?


It's a clever way to make a multi-statement macro appear to work like

a function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?).


I think if you remove the semicolon after DO_AMGIC_STUFF(quux)
it will both compile and executes the desired way.
Then why to use
do { } while(0);
Note: it is also costlier as there is an extra test at the end of loop.
Is there any case where we can't go without using do { } while(0);

Nov 14 '05 #18

P: n/a
ju**********@yahoo.co.in scribbled the following:
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
> What is the use of the above do-while. Is it simply a style issue. > Since the above same condition will applied with out a do-while(0); > because the loop executes only once. I went through the faq section 10.4 > but I am little understood. Any answers please?


It's a clever way to make a multi-statement macro appear to work like

a
function.
Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?).

I think if you remove the semicolon after DO_AMGIC_STUFF(quux)
it will both compile and executes the desired way.
Then why to use
do { } while(0);
Note: it is also costlier as there is an extra test at the end of loop.
Is there any case where we can't go without using do { } while(0);


I just explained above why to use the semicolon. It's to make the macro
appear like a function.
You don't call functions like this:
do_magic_stuff(quux)
You call them like this:
do_magic_stuff(quux);
Why should the syntax be any different for macros? If you ever decide to
change the macro to a function, or the function to a macro, you would
have to change the syntax in every place it is invoked. Plus it lessens
the consistency, making the code harder to read.
The point is, of course it's possible to avoid using do { } while(0) by
not using the semicolon at the end of the invocation, but it's easier
this way.
And it's not necessarily costlier to have an extra test at the end of
the loop. A clever compiler might deduce that the test is never going to
succeed, and thus omit it entirely, causing no performance loss
whatsoever.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 14 '05 #19

P: n/a
Thanx .....

Nov 14 '05 #20

P: n/a
On 2005-01-26 21:36, sathyashrayan wrote:
Joona I Palaste wrote:
sathyashrayan <lo****************@nomail.com> scribbled the following:
What is the use of the above do-while. Is it simply a style issue.
Since the above same condition will applied with out a do-while(0);
because the loop executes only once. I went through the faq section
10.4 but I am little understood. Any answers please?


It's a clever way to make a multi-statement macro appear to work like
a function. Consider:

#define DO_MAGIC_STUFF(x) {foo(x); bar(x);}
if (willDoMagic)
DO_MAGIC_STUFF(quux);
else
printf("Won't do magic stuff!\n");

This won't compile (exercise: why?). However, change the definition
to:

#define DO_MAGIC_STUFF(x) do {foo(x); bar(x);} while (0)

and it will compile (exercise: why?).


I have got zero marks in both of the exercises!?


Do the expansion of DO_MAGIC_STUFF(quux) in your head, or use a
preprocessor on the source posted by Joona I Palaste. Then the question
is transformed to:

- Why does the following fail to compile?

if (willDoMagic)
{foo(x); bar(x);};
else
printf("Won't do magic stuff!\n");

Hint: pay very close attention to semicolons.

There are also a few problems with macros that use do{ ... } while (0)
_and_ are expected to work "exactly" like functions (i.e. return a
meaningful value), as Lawrence Kirby has noted.

AFAIK, there is no way to guarantee that a compound statement has a
value. Thus, you cannot "return" the value of set_foo() in code like
this:

int set_foo(int *p, int val)
{
if (p == NULL)
return (-1);
*p = val;
return (0);
}

#ifdef DEBUGGING
#define SETFOO(p,x) do {
int retval;
retval = set_foo((p), (x));
if (retval == -1)
fprintf(stderr, "set_foo: Oops!\n");
} while (0)
#else
#define SETFOO(p,x) set_foo((p), (x))
#endif

The best way I know, until now, to actually _return_ a value is by using
a real function as a wrapper:

int set_foo(int *p, int val)
{
if (p == NULL)
return (-1);
*p = val;
return (0);
}

#ifdef DEBUGGING
#define SETFOO(p,x) my_set_foo((p), (x))

int my_set_foo(int *px, int x)
{
int retval;

retval = set_foo(px, x);
if (retval == -1)
fprintf(stderr, "set_foo: Oops!\n");
return (retval);
}
#else /* !DEBUGGING */
#define SETFOO(p,x) set_foo((p), (x))
#endif /* DEBUGGING */

Note, though, that this uses an extra function call, so in many cases
the overhead may not be acceptable.

- Giorgos

Nov 14 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.