很久沒有碰c/c++了, 近兩年都是java為主, 偶爾用一下c/c++
(a<b<c) 這種寫法我以前從來沒有用過. 今天突然有網友提起來, 我習慣性的在腦海中把其翻譯成
(a<b && b<c) 釀成了一些小錯誤. 我悔過了.
網友給我扔來了c99的標準
§6.5.9 第86頁 89) The expression a<b<c is not interpreted as in ordinary mathematics. As the syntax indicates, it
means (a<b)<c; in other words, ‘‘if a is less than b, compare 1 to c; otherwise, compare 0 to c’’.
意思也就是說. (a<b<c) 按照從左到右的方式開始運算
1.( (a<b) < c )
2.a<b ? TRUE : FALSE;
3.所以, 最終表達式運算的是 (1<c) 或者 (0<c)
C語言中. 雖然任何不等于0的數為真. 但是實際在宏定義中 TRUE == 1 FALSE == 0;