本文翻译自:do { … } while (0) — what is it good for? [duplicate]

Possible Duplicate: 可能重复:
Why are there sometimes meaningless do/while and if/else statements in C/C++ macros? 为什么在C / C ++宏中有时会出现无意义的do / while和if / else语句?

I've been seeing that expression for over 10 years now. 我已经看了十多年了。 I've been trying to think what it's good for. 我一直在努力思考它有什么好处。 Since I see it mostly in #defines, I assume it's good for inner scope variable declaration and for using breaks (instead of gotos.) 因为我主要在#defines中看到它,所以我认为它对内部范围变量声明和使用中断(而不是gotos)有好处。

Is it good for anything else? 对其他事情有好处吗? Do you use it? 你用它吗?


#1楼

参考:https://stackoom.com/question/14xu/做-而-有什么好处-重复


#2楼

It is a way to simplify error checking and avoid deep nested if's. 这是一种简化错误检查并避免深层嵌套if的方法。 For example: 例如:

do {// do somethingif (error) {break;}// do something elseif (error) {break;}// etc..
} while (0);

#3楼

Generically, do / while is good for any sort of loop construct where one must execute the loop at least once. 通常, do / while适用于任何类型的循环构造,其中必须至少执行一次循环。 It is possible to emulate this sort of looping through either a straight while or even a for loop, but often the result is a little less elegant. 它可以模仿这种通过的直循环的while甚至for循环,但结果往往是有点不太优雅。 I'll admit that specific applications of this pattern are fairly rare, but they do exist. 我承认这种模式的具体应用是相当罕见的,但确实存在。 One which springs to mind is a menu-based console application: 让人想到的是一个基于菜单的控制台应用程序:

do {char c = read_input();process_input(c);
} while (c != 'Q');

#4楼

It helps grouping multiple statements into a single one, so that a function-like macro can actually be used as a function. 它有助于将多个语句分组到一个语句中,因此类似函数的宏实际上可以用作函数。 Suppose you have 假设你有

#define FOO(n)   foo(n);bar(n)

and you do 你也是

void foobar(int n){if (n)FOO(n);
}

then this expands to 然后这扩大到

void foobar(int n){if (n)foo(n);bar(n);
}

Notice that the second call (bar(n)) is not part of the if statement anymore. 请注意,第二个调用(bar(n))不再是if语句的一部分。

Wrap both into do{}while(0), and you can also use the macro in an if statement. 将两者都包装到do {} while(0)中,也可以在if语句中使用宏。


#5楼

It's the only construct in C that you can use to #define a multistatement operation, put a semicolon after, and still use within an if statement. 它是C中唯一可用于#define多语句操作,后面加分号,仍然在if语句中使用的结构。 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 if statement would require that you omit the semicolon, which is counterintuitive: if语句中使用它将要求您省略分号,这是违反直觉的:

if (condition)FOO(x)
else...

If you define FOO like this: 如果你这样定义FOO:

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

then the following is syntactically correct: 那么以下语法是正确的:

if (condition)FOO(x);
else....

#6楼

It is interesting to note the following situation where the do {} while (0) loop won't work for you: 有趣的是,要注意以下情况:在做{}而(0)循环不会为你工作:

If you want a function-like macro that returns a value, then you will need a statement expression : ({stmt; stmt;}) instead of do {} while(0): 如果你想要一个返回值的类函数宏,那么你将需要一个语句表达式 :({stmt; stmt;})而不是do {} while(0):


#include <stdio.h>#define log_to_string1(str, fmt, arg...) \do { \sprintf(str, "%s: " fmt, "myprog", ##arg); \} while (0)#define log_to_string2(str, fmt, arg...) \({ \sprintf(str, "%s: " fmt, "myprog", ##arg); \})int main() {char buf[1000];int n = 0;log_to_string1(buf, "%s\n", "No assignment, OK");n += log_to_string1(buf + n, "%s\n", "NOT OK: gcc: error: expected expression before 'do'");n += log_to_string2(buf + n, "%s\n", "This fixes it");n += log_to_string2(buf + n, "%s\n", "Assignment worked!");printf("%s", buf);return 0;
}

做{...}而(0) - 有什么好处? [重复]相关推荐

  1. 怎样在一只股票上做T+0?

    今天不聊前端,聊股票! A股市场里真正厉害的一种人:股市最全"做T"攻略,10分钟从入门到进阶! 无论短线.中线,经常做T,远比持股不动要好很多很多;尤其在大盘震荡.下跌期间,盘中 ...

  2. 安全审计报告_企业做税审的五大好处!税审报告和审计报告的区别

    税务审计报告是税务审计人员根据税务审计的相关法律.法规,对被审计单位的纳税行为和纳税事项进行评估,并发表意见的书面文件.(咨询:13510890028,肖) 一.企业做税审的五大好处: 1.可弥补亏损 ...

  3. 惠普 HPE D380 G9 做raid1+0并安装ESXI6.7

    一.惠普 HPE D380 G9 做raid1+0并安装ESXI6.7 最近公司上架一批服务器,品牌:惠普,型号D380 G9,虽然不是最新款的机器,配置还是可以的,网上看了几篇安装步骤,都不太详细, ...

  4. 长尾词,为什么SEOer都要做长尾词?做长尾词有什么好处?

    为什么SEOer都要做长尾词?     SEOer所提到的长尾关键词,一般特点是围绕核心关键词展开的,它们通常是用户精确搜索的词语.长尾关键词是最能够体现用户搜索意图的,通常具有高转化率.     做 ...

  5. 复杂的事情简单做,你就是专家; 简单的事情重复做,你就是行家; 重复的事情用心做,你就是赢家。...

    复杂的事情简单做,你就是专家: 简单的事情重复做,你就是行家: 重复的事情用心做,你就是赢家. 转载于:https://www.cnblogs.com/javalyy/p/10647480.html

  6. 做WEB2.0网站可以参考的的十九条规则!

    做WEB2.0网站可以参考的的十九条规则 以下是我们团队即将做遵循这写内容来做的东西,拿出来与大家分享下,也希望通过交流法: 一.首先是原创,是内容的提供,让用户成为网站的主人而不是网站的客户: (一 ...

  7. python对新媒体运营的帮助_做新媒体运营有什么好处

    现今,无论是传统企业还是互联网公司,一说起网络营销,肯定会想到新媒体营销,为什么新媒体营销会这么火呢,下面让小编为你细细道来. 一.新媒体运营:个人 1. 内容付费时代的到来 这个可以从一些大咖案例得 ...

  8. 什么是SEO,企业做SEO优化有哪些好处

    提到SEO,很多人可能都知道是搜索引擎优化.在互联网的不断发展下,SEO已经不仅仅局限于常见的搜索引擎平台了,因为有搜索的地方就可以有SEO的存在.下面,本篇文章将介绍什么是SEO,企业做SEO优化有 ...

  9. 学平面设计能做什么?有什么好处?

    本文由:"学设计上兔课网"原创,图片素材来自网络,仅供学习分享 学平面设计能做什么?有什么好处?平面设计师的就业范围是非常广泛的,平面设计是所有一切设计的源头,与其它大部分的设计也 ...

  10. 设计一套随机生成四位密码的程序,范围0~~9,不能出现重复的情况

    //设计一套随机生成四位密码的程序,范围0~~9,不能出现重复的情况 function num( ){var arr= [];while(true){var isflag=true;var num1 ...

最新文章

  1. nginx配置http、https访问,nginx指定ssl证书,阿里云腾讯云华为云设置nginx https安全访问
  2. linux(armv7/8)下gdb的安装及查看方法
  3. LoadRunner12使用教程(三)——Action迭代
  4. 【c++】 类与对象
  5. 老友会 | 情怀与时光不期而遇的深情大趴(现场快讯)
  6. 用WPF开发Mapxtreme-- 在.net 4.0 下面开发WPF mapxtreme 程序
  7. Spring Boot集成CKFinder
  8. pandas输出到excel_python读写excel等数据文件方法汇总
  9. CGCKD2021大会报告整理(4)--风格迁移
  10. 修复2008r2 rpc服务器,windows server 2008R2 复制问题(RPC服务不可用)
  11. android 模仿微信布局,【Android初学者】框架布局:仿微信页面制作
  12. OSChina 周二乱弹 ——有时醒来发现身边是不同的姑娘
  13. Linux系统重要快捷键 Shell 常用通配符
  14. Linux 系统服务之间 实现免密登陆
  15. java获取数据库列的别名_java – JDBC ResultSet获取具有表别名的列
  16. python中series是什么_pandas中的series数据类型详解
  17. 正常邮件被误判为垃圾邮件
  18. CES2013前瞻:1080p屏幕手机集中爆发
  19. PyTorch实现Word2Vec
  20. Android studio制作简单微信界面

热门文章

  1. Kneron用“重组”方案精简深度学习算法,芯片产品明年开售
  2. mysql 无法启动时,或尝试mysqld_safe --user=root
  3. Android学习2--项目文件列表简单分析
  4. erlang 常用函数
  5. 使用Linux好还是unix好
  6. (转)马云的江湖 PK 史玉柱的兵法
  7. GStreamer基础教程01 - Hello World
  8. Linux文件默认权限——umask
  9. 输入一个日期,计算这个日期与 2018-03-12差多少天;
  10. 自定义标签TLD文件中,rtexprvalue子标签的意思