在csh中,为什么4-3 + 1 == 0? [英] In csh, why does 4 - 3 + 1 == 0?

查看:82
本文介绍了在csh中,为什么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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆