使用do {...} while(0) [英] use of do{...}while(0)

查看:75
本文介绍了使用do {...} while(0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上述做什么用途。这只是一个样式问题。

由于上述相同条件将应用do-while(0);

因为循环只执行一次。我经历了faq部分10.4

但我很少被人理解。请问有什么答案吗?

-

组合是国际象棋的核心


A.Alekhine


邮寄地址:

sathyashrayan at gmail DOT com

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

推荐答案

sathyashrayan <螺**************** @ nomail.com>潦草地写着以下内容:
sathyashrayan <lo****************@nomail.com> scribbled the following:
上述do-while有什么用处。它只是一个样式问题。
由于上述相同的条件将应用do-while(0);
因为循环只执行一次。我经历了常见问题10.4节,但我很少被人理解。请问有什么答案吗?
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 DO_MAGIC_STUFF(x){foo(x); bar(x);}

if(willDoMagic)

DO_MAGIC_STUFF(quux);

else

printf( 不会做神奇的东西!\ n;);


这不会编译(练习:为什么?)。但是,将定义

更改为:


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


它会编译(练习:为什么?)。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\ ------------------------------------------ --------------规则! -------- /

我希望我们认识的人会死,所以我们可以给他们留下鲜花。

- 一个6岁的孩子女孩,在墓地里看到鲜花



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


sathyashrayan写道:
sathyashrayan wrote:
上面做什么用途。它只是一个样式问题。
由于上述相同的条件将应用do-while(0);
因为循环只执行一次。我经历了常见问题10.4节,但我很少被人理解。请问有什么答案吗?
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?




10.4的答案文本可能是一个很好的解释,如果您了解上下文,则需要b / b
。它是关于编写一个C语言宏,

有效地扩展到几个独立的C语句中。


只是在心理上思考做什么{... } while(0)包装器正在做,你会看到
看看技巧是怎么回事工作。


- Larry Weiss



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


2005年1月26日星期三15:16:16 +0000,Joona I Palaste写道:
On Wed, 26 Jan 2005 15:16:16 +0000, Joona I Palaste wrote:
sathyashrayan< lo **************** @ nomail.com>潦草地写着以下内容:
sathyashrayan <lo****************@nomail.com> scribbled the following:
上述do-while有什么用处。它只是一个样式问题。
由于上述相同的条件将应用do-while(0);
因为循环只执行一次。我经历了常见问题10.4节,但我很少被人理解。请问有什么答案吗?
这是一个聪明的方法,使多语句宏看起来像
函数。
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.




不是这样的功能,一个声明要求;最后以

完成它。

考虑:

#define DO_MAGIC_STUFF(x){foo(x); bar(x);} if(willDoMagic)
DO_MAGIC_STUFF(quux);


你是对的,虽然它很可能出现在类似函数的

宏中。

else
printf(不要做魔术!\ n);

这不会编译(练习:为什么?)。但是,将定义更改为:
#define DO_MAGIC_STUFF(x)do {foo(x); bar(x);} while(0)

它会编译(练习:为什么?)。



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


这篇关于使用do {...} while(0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆