In csh, why does 4 - 3 + 1 == 0?

前端 未结 3 801
猫巷女王i
猫巷女王i 2021-01-17 11:11
#!/bin/csh

@ cows = 4 - 3 + 1
echo $cows

This simple csh script when run produces \"0\" for output when I\'d expect \"2\".

~root:          


        
相关标签:
3条回答
  • 2021-01-17 11:17

    Operator grouping. It's reading the operation as 4 - (3 + 1), as opposed to (4 - 3) + 1.

    0 讨论(0)
  • 2021-01-17 11:23

    The + and - operators are right-associative in csh. This means that '4 - 3 + 1' is evaluated as '4 - (3 + 1)'.

    0 讨论(0)
  • 2021-01-17 11:35

    While you are expecting the operators to be left associative, they are right associative in csh, so it's evaluated as 4-(3+1)

       -
      / \
     /   \
    4     +
         / \
        3   1
    
    0 讨论(0)
提交回复
热议问题