do {...} while (0) in macros 【转】
来源:互联网 发布:数据科学实战 微盘 编辑:程序博客网 时间:2024/05/29 15:46
【转】原文:https://www.pixelstech.net/article/1390482950-do-%7B-%7D-while-%280%29-in-macros
If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease your work if used correctly. However, if you don't define macros carefully, they may bite you and drive you crazy. In many C programs, you may see a special macro definition which may seem not so straightforward. Here is one example:
#define __set_task_state(tsk, state_value) \
do
{ (tsk)->state = (state_value); }
while
(0)
There are many this kind of macros which uses do{...}while(0) in Linux kernels and other popular C libraries. What's the use of this macro? Robert Love from Google(previously worked on Linux kernel development) gives us the answer.
do{...}while(0) is the only construct in C that lets you define macros that always work the same way, so that a semicolon after your macro always has the same effect, regardless of how the macro is used (with particularly emphasis on the issue of nesting the macro in an if without curly-brackets). For example:
#define foo(x) bar(x); baz(x)
Later you may call:
foo(wolf);
This will be expanded to:
bar(wolf); baz(wolf);
This is the expected output. Next let's see if we have:
if
(!feral)
foo(wolf);
The expansion may not be what you expect. The expansion may be:
if
(!feral)
bar(wolf);
baz(wolf);
It isn't possible to write multistatement macros that do the right thing in all situations. You can't make macros behave like functions—without do/while(0).
If we redefine the macro with do{...}while(0), we will see:
#define foo(x) do { bar(x); baz(x); } while (0)
Now, this statement is functionally equivalent to the former. The do ensures the logic inside the curly-brackets executes, the while(0) ensures that happens but once. Same as without the loop.For the above if statement, it will be :
if
(!feral)
do
{ bar(wolf); baz(wolf); }
while
(0);
Semantically, it's the same as:
if
(!feral) {
bar(wolf);
baz(wolf);
}
You might rejoin, why not just wrap the macro in curly-brackets? Why also have the do/while(0) logic? For example, we define the macro with curly bracket:
#define foo(x) { bar(x); baz(x); }
This is fine for the above if statement, but if we have below statement:
if
(!feral)
foo(wolf);
else
bin(wolf);
The expanded code will be :
if
(!feral) {
bar(wolf);
baz(wolf);
};
else
bin(wolf);
This is a syntax error.
In conclusion, macros in Linux and other codebases wrap their logic in do/while(0) because it ensures the macro always behaves the same, regardless of how semicolons and curly-brackets are used in the invoking code.
- do {...} while (0) in macros 【转】
- What is the purpose of using do {...} while (0) in macros?
- Do-While and if-else statements in C/C++ macros
- Why are there sometimes meaningless do/while and if/else statements in C/C++ macros?
- do-while in Shell
- do...while(0)-妙用 (转...)
- 【转】do...while(0)的妙用
- do...while(0)的妙用 (转)
- do while(0) 用法(转)
- do...while(0)的妙用(转)
- [精编][转]do{}while(0)宏详解
- do...while(0)
- do{}while(0)
- [转载]Do{}While(0)
- do {} while (0) 用法
- do-----while(0)
- do...while(0)
- do{}while(0)妙用
- 【数据库视频】触发器
- Java volatile关键字可见性分析
- oracle 查看隐藏参数
- Java String操作及类型转换
- ZigBee_CC2530_06H_中断的使用
- do {...} while (0) in macros 【转】
- QT 读取文件夹下所有文件
- 【ES6】class
- 邂逅明下 HDU
- Java集合类基础(List,Set,Map)
- 大公司的通病 VS 小公司的通病
- 常见反函数、反函数导数(微分)公式
- 461. Hamming Distance
- 广告图片随滚动条,上下移动做相应的改变,覆盖或者顶上去