`
yaojingguo
  • 浏览: 208021 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

What's the use of do while(0) when we define a macro?

 
阅读更多

Sample code:

 

#include <stdio.h>

#define INIT do { \
	printf("line1\n"); \
} while (0)

#define INIT_ { \
	printf("line1\n"); \
}

void correct() {
	if (1) 
		INIT;
	else
		printf("here is the else\n");
}

void wrong() {
 if (1) 
	 INIT_; // compile error
 else
	 printf("here is the else\n");
}

int main(int argc, const char *argv[]) {
	if (1) 
	{ printf("then\n"); }; // compile error
	else
		printf("else\n");
}

 

Refer to http://stackoverflow.com/questions/923822/whats-the-use-of-do-while0-when-we-define-a-macro

 

 

分享到:
评论

相关推荐

    SSD7 选择题。Multiple-Choice

    (b) the name of the table, the names of the table's attributes, the data types of the table's attributes, the formats of the table's attributes, and the maximum number of rows that the table can have...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    All of a project's header files should be listed as descentants of the project's source directory without use of UNIX directory shortcuts . (the current directory) or .. (the parent directory). For...

    Java邮件开发Fundamentals of the JavaMail API

    The version of the JavaMail API you want to use affects what you download and install. All will work with JDK 1.1.6+, Java 2 Platform, Standard Edition (J2SE) version 1.2.x, and J2SE version 1.3.x....

    The Art of SQL

    There used to be a time when what is known today as "Information Technology" or IT was less glamorously known as "Electronic Data Processing." And the truth is that for all the buzz about trendy ...

    do…while(0)的妙用详细解析

    在C++中,有三种类型的循环语句:for, while, 和do…while, 但是在一般应用中作循环时, 我们可能用for和while要多一些,do…while相对不受重视。但是我发现了do…while的一些十分聪明的用法,不是用来做循环,而是...

    how to make sense of any mess

    I define the word “mess” the same way that most dictionaries do: “A situation where the interactions between people and information are confusing or full of difficulties.” — Who doesn’t bump up...

    JSP Simple Examples

    We use the if condition to check if the particular condition is true then it should perform a certain task, and if a particular condition is not true then it should do some other tasks. ...

    微软内部资料-SQL性能优化2

    Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...

    微软内部资料-SQL性能优化5

    If there is no clustered index, there is a sysindexes row for the table with an indid value of 0, and that row will keep track of the address of the first IAM for the table. The IAM is a giant bitmap...

    计算机网络第六版答案

    14. If the two ISPs do not peer with each other, then when they send traffic to each other they have to send the traffic through a provider ISP (intermediary), to which they have to pay for carrying ...

    rfc全部文档离线下载rfc1-rfc8505

    terminals under the control of a remote HOST, the problem becomes more severe. We must look for some method which allows us to use our most sophisticated equipment as much as possible as if we were...

    C Programming

    - **Making Multiple Decisions**: Explanation of how the `switch` statement works and when it's appropriate to use it instead of nested `if` statements. #### Strings The document explains strings, ...

    You can define up to 4000 styles in a .xls workbook is a wrong excepiton

    The maximum number of cell styles was exceeded. You can define up to 4000 styles in a .xls workbook is a wrong excepiton 将抛异常的方法去掉,是程序继续执行,生成excel 。本来想免费,但是苦于没积分所以...

    Microservice-Architecture-Aligning-Principles-Practices-and-Culture.pdf

    The companies we talked to offered a wide range of services including live video and audio streaming service, foundation-level virtual services in the cloud, and support for classic brick-and-mortar ...

    a letter and a number

    we define f(A) = 1, f(a) = -1, f(B) = 2, f(b) = -2, ... f(Z) = 26, f(z) = -26; Give you a letter x and a number y , you should output the result of y+f(x). 输入 On the first line, contains a number T...

Global site tag (gtag.js) - Google Analytics