[問題] 關於遞增符號和bitwise問題

看板C_and_CPP作者 (wodada)時間11年前 (2013/05/02 18:20), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/1
*[36m開發平台(Platform): (Ex: VC++, GCC, Linux, ...) linux 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): bitwise與遞增符號結合 餵入的資料(Input):預期的正確結果(Expected Output): b= 1 test 2 c= -1 test 3 a= 1 test 4 c= 0 b= 2 test 5 錯誤結果(Wrong Output): 只想了解答案為何跟我想的不一樣 程式碼(Code):(請善用置底文網頁, 記得排版) 1 #include <stdio.h> 2 int main(int argc,char* argv[]) 3 { 4 int a=0; 5 int b=1; 6 int c=-1; 7 8 if(a && ++b) 9 printf("test 1\n"); 10 printf("b= %d\n",b); 11 12 if(b || ++c) 13 printf("test 2\n"); 14 printf("c= %d\n",c); 15 16 17 if(b && ++a) 18 printf("test 3\n"); 19 printf("a= %d\n",a); 20 21 22 if(++c || ++b) 23 printf("test 4\n"); 24 printf("c= %d\tb= %d\n",c,b); 25 26 if(!c) 27 printf("test 5\n"); 28 return 0; 29 } 補充說明(Supplement): 末學將這程式碼用筆想了幾遍,卻對程式碼第9,10行和第14,15行無法列印 感到疑惑,因為正常來說不管是AND或OR完之後應該都會列印出來 可是卻偏偏沒有出現test 1和test 2 想請各位前輩解惑 感謝!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.3.96

05/02 18:28, , 1F
這不叫bitwise 答案在你上第六篇
05/02 18:28, 1F

05/02 18:30, , 2F
然後你怎麼會覺得第9行會印?
05/02 18:30, 2F

05/02 18:45, , 3F
抱歉,是邏輯運算子.....= =
05/02 18:45, 3F

05/02 18:50, , 4F
我知道了,謝謝a大提醒
05/02 18:50, 4F

05/03 02:45, , 5F
14 15行有印出來不是嘛 ._.? 看到b不是false就會直接印了
05/03 02:45, 5F
文章代碼(AID): #1HWZuAmT (C_and_CPP)