do{...}while(0)what is it good for?
2013-10-01 17:37
477 查看
I assume it's good for inner scope variable declaration and for using breaks (instead of gotos.)
It's the only construct in C that you can use to
multistatement operation, put a semicolon after, and still use within an
An example might help:
Even using braces doesn't help:
Using this in an
would require that you omit the semicolon, which is counterintuitive:
If you define FOO like this:
then the following is syntactically correct:
It is a way to simplify error checking and avoid deep nested if's. For example:
It's the only construct in C that you can use to
#definea
multistatement operation, put a semicolon after, and still use within an
ifstatement.
An example might help:
#define FOO(x) foo(x); bar(x) if (condition) FOO(x); else // syntax error here ...;
Even using braces doesn't help:
#define FOO(x) { foo(x); bar(x); }
Using this in an
ifstatement
would require that you omit the semicolon, which is counterintuitive:
if (condition) FOO(x) else ...
If you define FOO like this:
#define FOO(x) do { foo(x); bar(x); } while (0)
then the following is syntactically correct:
if (condition) FOO(x); else ....
It is a way to simplify error checking and avoid deep nested if's. For example:
do { // do something if (error) { break; } // do something else if (error) { break; } // etc.. } while (0);
相关文章推荐
- do { … } while (0) what is it good for?
- VS 2005 Web Project System: What is it and why did we do it?
- What is Logical Volume Management and How Do You Enable It in Ubuntu?
- What is a good buffer size for socket programming?
- What is the best free memory leak detector for a C/C++ program and its plug-in DLLs
- Cocoa Programming for Mac OS X 第一章(What Is It?)摘录
- which language is suitable for what to do
- Coconut Oil Acne Treatment - Is it Good For Skin Acne?
- What is virtual memory, how is it implemented, and why do operating systems use it?
- 安装Visual Studio 2010时提示"The location specified for the help content store is invalid or you do not have access to it".
- Why SHRINKFILE is a very bad thing, and what to do about it.
- A ship is always safe at the shore - but that is not what it is built for.
- What is a good file manager for Linux?
- Requirement Traceability Matrix (RTM) – What Is RTM And Why Do We Need It?
- What is the PPA and How to do with it ?
- What is the purpose of using do {...} while (0) in macros?
- What is a "sosreport" and how do I create it in Red Hat Enterprise Linux 4.6 and later
- It is said that wars in the 21st century will be fought over water. Do you agree? What do you think can be done now to alleviate
- What is DMA and what does it do?