C++基础:C语言中##的用法

  C语言中##称为连接符,其功能是在带参数的宏定义中将两个子串(token)联接起来,从而形成一个新的子串。

  要注意下面的用法:

  1、

  [cpp]

  #include <stdio.h>

  #define debug(format, args...) fprintf(stderr, format, args)

  void main(void){

  debug("Test \n");

  return;

  }

  有的说这种情况下字符串后面会多一个逗号,但是我用gcc编译不通过;

  2、

  [cpp]

  #include <stdio.h>

  #define debug(format, args...) fprintf(stderr, format, ##args)

  //#define debug(format, args...) fprintf(stderr, format, args)

  void main(void){

  debug("Test \n");

  return;

  }

  这样可以编译通过,执行正确;

  3、

  [cpp]

  #include <stdio.h>

  //#define debug(format, args...) fprintf(stderr, format, ##args)

  #define debug(format, args...) fprintf(stderr, format, args)

  void main(void){

  debug("Test%d \n",1);

  return;

  }

  这样也正确;

    }, tim );

  EventKey ke=new EventKey();

  Thread1 demo1=new Thread1(ke) ;

  Thread2 demo2=new Thread2(ke) ;

  demo1.start();

  demo2.start();

  }

  }

  class Thread1 extends Thread{

  private EventKey ek;

  private int ko;

  public Thread1(EventKey e){

  ek=e;

  }

  public void run(){

  synchronized(this){

  while(true){

  ko=ek.next();

  System.out.println(Thread.currentThread()+"ko:"+ko);

  if(ko % 2 !=0 ){

  System.out.println("输出的是奇数");

  System.exit(0);

  }

  }}}

  }

  class Thread2 extends Thread{

  private EventKey ek;

  private int ko;

  public Thread2(EventKey e){

  ek=e;

  }

posted @ 2013-09-23 14:20  来自你520的末班车  阅读(507)  评论(0编辑  收藏  举报