Using if (!!(expr)) instead of if (expr)

前端 未结 5 1655
终归单人心
终归单人心 2021-02-03 17:05

While reading the example code provided by Texas Instruments for their SensorTag I came across the following snippet.

void SensorTagIO_processCharChangeEvt(uint8         


        
5条回答
  •  孤独总比滥情好
    2021-02-03 17:40

    Applying the logical not (!) operator twice has the purpose of normalising a value to be either 0 or 1. In a control expression of an if-statement, that doesn't make any difference. The if-statement only cares about the value being zero or nonzero, the little !! dance is completely useless.

    Some coding style guides might mandate this kind of dance, which could be the reason why the TI code you posted does it. I haven't seen any that do so though.

提交回复
热议问题