`
yaojingguo
  • 浏览: 210907 次
  • 性别: 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

 

 

分享到:
评论

相关推荐

    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...

    数位板压力测试

    When the user is viewing the device in its normal position, the coordinate origin will be at the lower left of the device. The coordinate system will be right-handed, that is, the positive x axis ...

    Debugging Malloc Lab: Detecting Memory-Related Errors

    In addition, it should print out the information about all of the corrupted blocks, using the macro #define PRINTERROR(int errorcode, char *filename, int linenumber), with errorcode equal to the ...

    2009 达内Unix学习笔记

    ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ 一、注意事项 命令和参数之间必需用空格隔开,参数和...

    acpi控制笔记本风扇转速

    the Switch() operator is within a while loop, causing an error on the second iteration. (BZ 460) Disassembler - fix for error emitted for unknown type for target of scope operator. Now, ignore it and...

    C++经典书籍高频词汇_不怕看不懂英文著作

    `define`, `macro`, `will`, `example`, `chapter`, `has`, `sequence`, `than`, `call`, `used`, `set`, `result`, `number`, `when`, `new`, `conversion`, `exception`, `library`, `its`, `other`, `input`, `...

Global site tag (gtag.js) - Google Analytics