在csh中,为什么4-3 + 1 == 0? [英] In csh, why does 4 - 3 + 1 == 0?
问题描述
#!/bin/csh
@ cows = 4 - 3 + 1
echo $cows
这个简单的csh脚本在运行时会在我期望为"2"时生成"0"作为输出.
This simple csh script when run produces "0" for output when I'd expect "2".
~root: csh simple.1
0
我做了很多查找,唯一想到的是,-"被理解为一元否定而不是减法,因此更改了运算符的优先级并以4-4而不是2 + 1结尾. 这样对吗?如果是这样,原因何在?如果没有...帮助!
I did a bunch of looking and the only thing I could think of was that the "-" was being read as a unary negation rather than subtraction, therefore changing operator precedence and ending up with 4 - 4 rather than 2 + 1. Is this correct? If so, any reason why? If not...help!
因此它们是正确的关联!这些运算符在C中不是正确的关联,对吗? C-Shell与C有什么不同吗?
So they're right associative! These operators are NOT right associative in C, are they? Is C-Shell that different from C?
推荐答案
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
这篇关于在csh中,为什么4-3 + 1 == 0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!