天天看點

c語言中的短路規則,在項目中的影響

寫法一:(不能得到想要的結果)
spn_kern_get_ifindex_by_fpga_xaui_index(slot, chip, (port_index || 25 == port_index) ? 0: 8, &outifindex[0]);

寫法二:(可以得到想要的結果)
spn_kern_get_ifindex_by_fpga_xaui_index(slot, chip, (1 == port_index || 25 == port_index) ? 0: 8, &outifindex[0]);
           

port_index取值範圍為0,1,24,25

期望:

0,24時,指派0

1,25時,指派8

分析:

寫法一,導緻port_index為1,24,25,時都指派0.(當port_index為24時,由于短路規則,不再計算||後面的條件,導緻不能得到期望結果)

寫法二,明确,port_index為1,25時指派0  

C/C