首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

if语句的两种方法出现不同结果,请指点,多谢

2012-02-15 
求助:if语句的两种方法出现不同结果,请指点,谢谢今天编写多线程C程序的时间,编写了如下语句:方式1:errcode

求助:if语句的两种方法出现不同结果,请指点,谢谢
今天编写多线程C程序的时间,编写了如下语句:
方式1:
errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]);
  if(errcode!=0)
  {
  fprintf(logfile,"Create @@@@@@@@ %d Pthread_create TCP:%s\n",inthID[i],strerror(errcode));
  fflush(logfile);
  //exit( 1);
  }

方式2:
if( (errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]) ))
  {
  fprintf(logfile,"Create @@@@@@@@ %d Pthread_create TCP:%s\n",inthID[i],strerror(errcode));
  fflush(logfile);
  //exit( 1);
  }
这两种方式,其中方式1 不执行括号里面的输出语句,而方式2却执行括号里面的输出语句。
但是在实际上,方式2中,pthread_create应该返回的是0吧?
那么if(errcode=0)应该不执行才对吧?这与实际运行情况不符合。
请各位给指点一下,看看具体是什么原因吧。谢谢了
期待各位的回复,谢谢

[解决办法]
方式1:
errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]); 
if(errcode!=0) //不同之处就是errcode!=0 用的是!=

方式2:
if( (errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]) )) )

没用!= 
改成if( !(errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]))) )
就一样了
[解决办法]

探讨
你明白这个结果的含义吗?

[解决办法]
赋值运算传统上是返回=右侧的值,不会返回真假,但是C/C++允许把一个非0值当作真,也就是说如果a=b的b是非0值,则a=b真,否则a=b假
探讨
引用:
方式1:
errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]);
if(errcode!=0) //不同之处就是errcode!=0 用的是!=

方式2:
if( (errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]) )) )

没用!=
改成if( !(errcode=pthread_create(&(dbthread[inthID[i]]),NULL,net159db,(void *)&inthID[i]))) )


[解决办法]
探讨
你测试证明啥?8、9楼不扯淡?…

热点排行