Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Can you provide an example, from any language, where a unary operator has a lower precedence than a binary operator?


Precedence does not matter, semantics do: pointer arithmetic is well defined, but taking the address of an arbitrary expression is not (and should not be, ever).


-3^5 = -(3^5)

Not to defend that guy or anything.


Unary - still has higher precedence in C (over the bitwise XOR operator).

In case you mean exponentiation, these expressions would always be equal regardless of the precedence of the operators.


(-3)^4 != -(3^4) when ^ means exponentiation.


It does for the expression given, silly. And again, in mathematical notation, unary - would take precedence over exponentiation so it's completely unclear what that person's example is supposed to mean and why it has upvotes.


I was actually originally going to say -3 * 5 = -(3 * 5), as a joke. But from what I could find online, it looks like unary minus traditionally falls below exponentiation in the order of operations for math. See http://mathforum.org/library/drmath/view/53194.html and Wikipedia: "In written or printed mathematics, the expression −3^2 is interpreted to mean −(3^2) = −9."


I think we have a QED here. :)


Obviously there are some cases where you should use parentheses. The reference operator just seems like a strange place to draw the line. I mean, to me it seems like the least confusing part of the quoted line.


Yeah, because it's impossible to be a skilled C programmer without knowing this stuff by heart.

/rolls eyes


There are plenty of confusing order-of-operation things in C, especially around pointers, but is "&x + 1" really one of them? I basically know nothing about this subject and it's still pretty clear that the & would take precedence. Would you balk at that expression if x were an int pointer?


I don't like guessing. "Yeah, I think it has high precedence" doesn't cut it any more than "Yeah, I think that is supposed to non-null" does. I try not to make assumptions.

Does this mean I write code so that it can be read by someone with little experience? Yes, generally. Does this mean I am constantly having to verify my assumptions, as if I don't know anything? Yes, it does. There are some things that I'll take a pass on verifying, but if elevates a problem and everything else checks out, I'm going to seart looking at the parts that I "know" are fine.


This particular case isn't that interesting because if the expression were equivalent to &(x+1) the compiler would complain about taking the address of a non-lvalue.

But ryanf, while not defending "that guy" (me), made my point for me: if I have to stop and think about whether -3^5 == -(3^5) because some hotshot didn't consider it manly to use parentheses, nobody wins.

If I were writing that expression I'd use (yes, completely unnecessary) parentheses around &a out of habit... the same habit that wouldn't let me write -3^5. Either way, it doesn't burn any more cycles.


no, but you will certainly never be a skilled C programmer if you don't reckonize a reference

even me a noob in C I can see that it is indeed a reference (as I said I m a noob =))


Sure. In J:

       i.2+$i.3
    0 1 2 3 4
That's "i. 2 + $ i. 3" (iota 2 plus shape_of iota 3), which parses as i.(2 + $(i. 3)). iota returns an array from 0 to n, + adds, shape_of returns the dimensions of a array/matrix/n-dimensional matrix/etc. + is a binary operator ("dyad"), i. and $ are unary operators ("monads", no relation to Haskell).


(Oops. Misread your comment, and missed the edit window. Nevermind.)




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: