954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Priority in Operators

Primary (x) x.y f(x) a[x] x++ x--new typeof sizeof checked unchecked
Unary + -~ ! ++x --x (T)x
Multiplicative * / %
Additive + -
Shift << >>
Relational < > <= >= is as
Equality == !=
Logical AND&
Logical XOR^
Logical OR|
Conditional AND&&
Conditional OR||
Conditional c?x:y
Assignment = += -= *= /= %= <<= >>= &= ^= |=
Operators on the same level are evaluated from left to right

For better programming skills remember above priority in operators.

Thanks,
Anuj

anujtripathi
Newbie Poster
7 posts since Dec 2008
Reputation Points: 7
Solved Threads: 1
 

For more effective programming, don't try and memorise this list; bracket expressions appropriately so that any programmer can read them whether or not they have also memorised this list (practically nobody has). OK, it's probably safe to assume that most programmers know that * and / have priority over + and -. But beyond that, nobody generally remembers or cares that, say, AND has higher precedence over XOR. And nobody is going to think you're a "better programmer" because you've pointlessly memorised this list.

N.B. This list isn't "handed down by God". It's valid for C, Java and other languages that have deliberately chosen to use the same order of precedence. But some other language could choose to use a different order.

neilcoffey
Junior Poster in Training
53 posts since Dec 2008
Reputation Points: 120
Solved Threads: 7
 

> For better programming skills remember above priority in operators
I've been doing C for 20 years, and I still can't be bothered to remember it.
The same goes for a large part of the standard C library. I know what functions exist, but I don't remember parameter orders for every single one of them (that's what man pages are for).

I know roughly where everything goes, but if the expression becomes too complex (which is a hint in itself), then I just use ( )

Besides, if you're writing code commercially, you have to figure that at some point, someone who knows less C than you will be maintaining it at some point. So it pays to be not overly clever with your code.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

++ to both of the above posts.
If you're writing a lot of code that depends on knowing that list well, no one will want to work with you (including yourself when you are trying to track down your own bugs 3 months later).

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You