当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
C宏定义探析
发布时间:2010/8/4 10:33:31 来源:城市学习网 编辑:ziteng
  C语言中,预处理器功能:
  1. #include <>or" " 的头文件替换
  2.#define <identifier> <replacement token list>   对象替换(object-like)
  对象替换以第一个空格为分割,之后的为replacement token list
  3.#define <identifier>(<parameter list>) <replacement token list>     函数替换(function-like)
  函数替换 <identifier>(<parameter list>)之间不能有任何空白符。但是调用的时候可以在之间有空格。
  函数替换的时候需要注意参数表的优先级和类型。如果替换块中需要用--';'是,用do{}while(0)封装,
  另外注意宏定义末尾不能有';'否则if-else语句的时候容易出错。
  4 #ifdefine等条件编译选项
  宏定义中比较容易搞错的是##与#的使用。
  ##是连接两个参数,
  #define MYCASE(item,id) \
  case id: \
  item##_##id = id;\
  break
  switch(x) {
  MYCASE(widget,23);
  }
  MYCASE(widget,23); 被扩展为
  case 23:
  widget_23 = 23;
  break;
  #是把参数变为字符串
  #define QUOTEME(x) #x
  printf("%s\n", QUOTEME(1+2));
  替换后==>
  printf("%s\n", "1+2");
  在使用##与#的时候,如果想使宏一定的参数也被宏替换(使用其值)
  而不是参数名字被使用,应该使用间接访问的方式。 [NextPage]   下面是两个例子:
  -----------------------------------------------------------------------------------------------------------
  enum {
  OlderSmall = 0,
  NewerLarge = 1
  };
  #define Older Newer
  #define Small Large
  #define _replace_1(Older, Small) Older##Small
  #define _replace_2(Older, Small) _replace_1(Older, Small)
  void printout( void )
  {
  // _replace_1( Older, Small ) becomes OlderSmall (not NewerLarge),
  // despite the #define calls above.
  printf("Check 1: %d\n", _replace_1( Older, Small ) );
  // The parameters to _replace_2 are substituted before the call
  // to _replace_1, so we get NewerLarge.
  printf("Check 2: %d\n", _replace_2( Older, Small ) );
  }
  results is:
  Check 1: 0
  Check 2: 1
  -----------------------------------------------------------------------------
  #define FOO bar
  #define QUOTEME_(x) #x
  #define QUOTEME(x) QUOTEME_(x)
  the code
  printf("FOO=%s\n", QUOTEME(FOO));
  扩展后==>
  printf("FOO=%s\n", "bar");
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved