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

#!/bin/csh @ cows = 4 - 3 + 1 echo $cows 

This simple csh script on startup creates a "0" for output when I expect a "2".

 ~root: csh simple.1 0 

I made a bunch of looks, and the only thing I could think of was that the β€œ-” was read as a unary negation, not a subtraction, so changing the operator’s priority and ending with 4-4, not 2 + 1. Is that right? If so, why? If not ... help!

Edit: that means they are associative right! These operators are NOT associative in C, right? Is C-Shell different from C?

+11
operators csh
Jun 17 '09 at 23:43
source share
3 answers

As long as you expect the operators to remain associative, they are right associative in csh , so it evaluates to 4-(3+1)

  - / \ / \ 4 + / \ 3 1 
+31
Jun 17 '09 at 23:51
source share

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

+20
Jun 17 '09 at 23:46
source share

Grouping of operators. It reads the operation as 4 - (3 + 1), unlike (4 - 3) + 1.

+3
Jun 17 '09 at 23:45
source share



All Articles