很久沒(méi)有碰c/c++了, 近兩年都是java為主, 偶爾用一下c/c++
(a<b<c) 這種寫法我以前從來(lái)沒(méi)有用過(guò). 今天突然有網(wǎng)友提起來(lái), 我習(xí)慣性的在腦海中把其翻譯成
(a<b && b<c) 釀成了一些小錯(cuò)誤. 我悔過(guò)了.
網(wǎng)友給我扔來(lái)了c99的標(biāo)準(zhǔn)
§6.5.9 第86頁(yè) 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’’.
意思也就是說(shuō). (a<b<c) 按照從左到右的方式開始運(yùn)算
1.( (a<b) < c )
2.a<b ? TRUE : FALSE;
3.所以, 最終表達(dá)式運(yùn)算的是 (1<c) 或者 (0<c)
C語(yǔ)言中. 雖然任何不等于0的數(shù)為真. 但是實(shí)際在宏定義中 TRUE == 1 FALSE == 0;