連等的if語句_小心編譯器

 UINT nR = GetRValue (clrDest);
UINT nG = GetGValue (clrDest);
UINT nB =GetBValue (clrDest);
// if(nR == nG == nB == 0)
if((nR == 0)&&(nG==0)&&(nB == 0)) 這兩條if語句竟然不是等價的。。

這是在vs2005編譯器下測試得到的情況,不知道其他的編譯器是不是這樣工作的。以後寫代碼一定要小心啊!

 

vs2005 測試代碼:

// testIf.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <windows.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
 UINT nR = 25;
 UINT nG = 25;
 UINT nB = 23;
 // if(nR == nG == nB == 0)
 if((nR == 0)&&(nG==0)&&(nB == 0))
 {
  printf("dddd");
 }
 else
 {
  printf("why_why");
 }
 return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章