Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6033
  • 博文数量: 2
  • 博客积分: 66
  • 博客等级: 民兵
  • 技术积分: 30
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-04 20:01
文章分类

全部博文(2)

文章存档

2012年(2)

我的朋友
最近访客

分类: LINUX

2012-06-19 09:05:45

        见到了神奇的do{....}while(0);结构通过学习才发现其运用的精妙之处。do{....}while(0);主要是用在宏展开时防止出现错误。
       一般情况我们使用#define定义一个宏比如这样:

点击(此处)折叠或打开

  1.  #include
  2.  #define print(x) printf ("%s\njust test;\n", x);\
  3.  printf ( "%s\nalso test;\n",x );
  4.  main()
  5.  {
  6.  print ("what's this?\n");
  7.  }
      当它编译的时候会是这个样子:
       

点击(此处)折叠或打开

  1. main()
  2. {
  3.  printf ("%s\njust test;\n", "what's this?\n"); printf ( "%s\nalso test;\n","what's this?\n" );;
  4. }
       这样子看着是没有错的,但是当我们如此调用print这个宏的时候编译就会出问题:

点击(此处)折叠或打开

  1. 1 #include<stdio.h>
  2.   2 #define print(x) printf ("%s\njust test;\n", x);\
  3.   3 printf ( "%s\nalso test;\n",x );
  4.   4 main()
  5.   5 {
  6.   6 int i = 0;
  7.   7 if ( i == 0 )                   <<<<<<<<<<<<<加入if....else....判断。
  8.   8     print ("what's this?\n");
  9.   9 else
  10.  10     printf ( "Nothing\n" );
  11.  11 }
     出错信息是这样的:

点击(此处)折叠或打开

  1. 7.c: In function ‘main’:
  2. 7.c:9:2: error: ‘else’ without a previous ‘if’
     实际的编译代码是这样的:

点击(此处)折叠或打开

  1. main()
  2. {
  3.  int i = 0;
  4.  if ( i == 0 )
  5.   printf ("%s\njust test;\n", "what's this?\n"); printf ( "%s\nalso test;\n","what's this?\n" );;      <<<<<<<<<<<<<<<<<<<问题出现在这一行
  6.  else
  7.   printf ( "Nothing\n" );
  8. }
    但是如果我们用do{.....}while(0)来改变这段代码:

点击(此处)折叠或打开

  1. 1 #include<stdio.h>
  2.   2 #define print(x) do{printf ("%s\njust test;\n", x);\
  3.   3 printf ( "%s\nalso test;\n",x );}while(0)    <<<<<<<<<<<加上do{....}while(0)
  4.   4 main()
  5.   5 {
  6.   6 int i = 0;
  7.   7 if ( i == 0 )
  8.   8 print ("what's this?\n");
  9.   9 else
  10.  10 printf ( "Nothing\n" );
  11.  11 }
     我们可以看到的编译的实际代码是:

点击(此处)折叠或打开

  1. main()
  2. {
  3.  int i = 0;
  4.  if ( i == 0 )
  5.   do{printf ("%s\njust test;\n", "what's this?\n"); printf ( "%s\nalso test;\n","what's this?\n" );}while(0);
  6.  else
  7.   printf ( "Nothing\n" );
  8. }
      这样就不会有错了。并且不直接使用{}的原因是宏展开时会在后面加上一个;因此只使用{}也是会出错的。比如:

点击(此处)折叠或打开

  1. 1 #include<stdio.h>
  2.   2 #define print(x) {printf ("%s\njust test;\n", x);\
  3.   3 printf ( "%s\nalso test;\n",x );}         <<<<<<<<<<<直接使用{}
  4.   4 main()
  5.   5 {
  6.   6 int i = 0;
  7.   7 if ( i == 0 )
  8.   8 print ("what's this?\n");
  9.   9 else
  10.  10 printf ( "Nothing\n" );
  11.  11 }
      宏展开后的代码是:

点击(此处)折叠或打开

  1. main()
  2. {
  3.  int i = 0;
  4.  if ( i == 0 )
  5.   {printf ("%s\njust test;\n", "what's this?\n"); printf ( "%s\nalso test;\n","what's this?\n" );};                <<<<<<<<出错在这
  6.  else
  7.   printf ( "Nothing\n" );
  8. }
        以上就是我总结的do{...}while(0)的精妙之处。


阅读(533) | 评论(1) | 转发(0) |
0

上一篇:初识list_entry

下一篇:没有了

给主人留下些什么吧!~~

最大行业软件2012-11-02 16:26:45

ADAPT_BUILDER_ABI_V2009_PLUS_BUILD2
ADAPTSOFT_ADAPT_BUILDER_V1.5.4
ADAPT_BUILDER_EX_V3.1
ADAPT_BUILDER_MAT_EX_V2.10.2
ADAPT_ABI_V4.50.3
ADAPT_PT_V8.0房屋与停车场结构里的梁、版、楼版系统之先进后拉预力软体
ADAPT_RC_V5.00.3
ARRIS.v9.0.112 建筑设计师设计软件
Better.Homes.and.Gardens.Interior.Designer.v7.05
Big.Hammer.Do.It.Yourself.Fence.Designer.v5 庭園設計
Big.Hammer.Do.It.Yourself.Patio.Designer.v5 內院設計
Broderbund_3D_Home_Landscape_Designer_Deluxe_5_READNFO, 庭