440,190 Members | 1,555 Online
Need help? Post your question and get tips & solutions from a community of 440,190 IT Pros & Developers. It's quick & easy.

# Operator Order

 P: n/a Hi, -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! Thanks Ravi Nov 13 '05 #1
13 Replies

 P: n/a On Tue, 23 Sep 2003 22:32:28 -0700, Ravi Uday wrote: Hi, [operator precedence question] Ravi That should be explained in your C book. When in doubt use parenthesis. Far less work than memorizing the operator precedence rules, far less chance of making a mistake, far less chance of someone reading your code misunderstanding. -- NPV "Linux is to Lego as Windows is to Fisher Price." - Doctor J Frink Nov 13 '05 #2

 P: n/a "Ravi Uday" wrote: Hi, I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order? That's not a statement, it's an expression. According to the operator precedence it's equivalent to: ((((7/9)*9)+6)-4) One way to evaluate that would be: 1st: 7/9 = 0 2nd: 0*9 = 0 3rd: 0+6 = 6 4th: 6-4 = 2 But if it gives the same result, the implementation is allowed to execute them in a different order or even not at all (such as to perform the calculation at compile time, and just insert a constant value). -Which of the following is evaluated first: a) && b) || c) ! Unary operators have higher precedence than binary operators. -- Simon. Nov 13 '05 #3

 P: n/a "Ravi Uday" wrote in message news:ec**************************@posting.google.c om... -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! I'm surprised there doesn't seem to be a reference to an operator precedence/associativity table in the FAQ, but a Google search for "C precedence table" will certainly give you the information you're looking for. -- #include char*f="#include %cchar*f=%c%s%c;%cint main(void){printf(f,10,34,f,34,10,10);return 0;}%c"; int main(void){printf(f,10,34,f,34,10,10);return 0;} Nov 13 '05 #4

 P: n/a ra*****@yahoo.com (Ravi Uday) wrote in message news:... Hi, -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! Thanks Ravi The operator ordering is given in Kerningham and Ritchie. also any online turorial like http://vwop.port5.com/beginner/ctuto...pressions.html should help you Is that u kumble?? -Char from ITSol Nov 13 '05 #5

 P: n/a Ravi Uday wrote: Hi, -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! Thanks Ravi Thank you for these intelligent questions, Ravi. 7/9*9+6-4 is evaluated like this: 1. 7/9 = 0 2. 0*9 = 0 3. 0+6 = 6 4. 6-4 = 2 <--- Final answer is 2. A technical explanation using common terms: Two things in C which influence the order in which operations are performed are "precedence" and "associativity." 1. "Precedence" is a ranking of operators. 2. "Associativity" is the direction in which operations are performed. a. "Left associativity" means to evaluate from left to right. b. "Right associativity" means to evaluate from right to left. The following is a chart from _C Programming: A Modern Approach_, by K.N. King. Precedence Name Symbol(s) Associativity 1 array subscripting [ ] left 1 function call ( ) left 1 structure and union member . -> left 1 increment (postfix) ++ left 1 decrement (postfix) -- left __________________________________________________ _________________________ 2 increment (prefix) ++ right 2 decrement (prefix) -- right 2 address of & right 2 indirection * right 2 unary plus + right 2 unary minus - right 2 bitwise complement ~ right 2 logical negation ! right 2 size sizeof right __________________________________________________ _________________________ 3 cast () right __________________________________________________ _________________________ 4 multiplicative * / % left __________________________________________________ _________________________ 5 additive + - left __________________________________________________ __________________________ 6 bitwise shift << >> left __________________________________________________ __________________________ 7 relational < > <= >= left __________________________________________________ __________________________ 8 equality == != left __________________________________________________ __________________________ 9 bitwise and & left __________________________________________________ __________________________ 10 bitwise exclusive or ^ left __________________________________________________ __________________________ 11 bitwise inclusive or | left __________________________________________________ __________________________ 12 logical and && left __________________________________________________ __________________________ 13 logical or || left __________________________________________________ __________________________ 14 conditional ?: right __________________________________________________ __________________________ 15 assignment = *= /= %= right += -= <<= >>= &= ^= |= __________________________________________________ ___________________________ 16 comma , left __________________________________________________ ___________________________ --Steve Nov 13 '05 #6

 P: n/a ra*****@yahoo.com (Ravi Uday) wrote in message news:... Hi, -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! Hey Mike I am using your line [ slightly modified ] hope you don't mind "Hello AOL Tech support, I need help to reboot my computer" -- Imanpreet Singh Arora imanpreet_arora AT yahoo DOT co DOT in Nov 13 '05 #7

 P: n/a In "Nils Petter Vaskinn" writes: On Tue, 23 Sep 2003 22:32:28 -0700, Ravi Uday wrote: Hi,[operator precedence question] RaviThat should be explained in your C book. Why do so many people believe that c.l.c is supposed to used as a replacement for consulting a C book? When in doubt use parenthesis. Far less work than memorizing the operatorprecedence rules, far less chance of making a mistake, far less chance ofsomeone reading your code misunderstanding. Good advice. However, any C programmer must be reasonably familiar with the operator precedence rules, to be able to correctly understand C code written by other people. The biggest gotcha is that few beginners have any doubt about the meaning of something like (i & 0xFF == 0x42), so they don't feel any need for extra parentheses. Dan -- Dan Pop DESY Zeuthen, RZ group Email: Da*****@ifh.de Nov 13 '05 #8

 P: n/a ra*****@yahoo.com (Ravi Uday) writes: -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! This is homework, isn't it? Look it up in your textbook. (I suspect what you're really looking for is operator precedence, not order of evaluation.) -- Keith Thompson (The_Other_Keith) ks*@cts.com San Diego Supercomputer Center <*> Schroedinger does Shakespeare: "To be *and* not to be" Nov 13 '05 #9

 P: n/a ra*****@yahoo.com (Ravi Uday) writes: -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! This is homework, isn't it? Look it up in your textbook. (I suspect what you're really looking for is operator precedence, not order of evaluation.) -- Keith Thompson (The_Other_Keith) ks*@cts.com San Diego Supercomputer Center <*> Schroedinger does Shakespeare: "To be *and* not to be" Nov 13 '05 #10

 P: n/a [snip] Two things in C which influence the order in which operations are performed are "precedence" and "associativity." 1. "Precedence" is a ranking of operators. 2. "Associativity" is the direction in which operations are performed. a. "Left associativity" means to evaluate from left to right. b. "Right associativity" means to evaluate from right to left. The following is a chart from _C Programming: A Modern Approach_, by K.N. King. Precedence Name Symbol(s) Associativity 1 array subscripting [ ] left 1 function call ( ) left 1 structure and union member . -> left 1 increment (postfix) ++ left 1 decrement (postfix) -- left __________________________________________________ _________________________ 2 increment (prefix) ++ right 2 decrement (prefix) -- right 2 address of & right 2 indirection * right 2 unary plus + right 2 unary minus - right Does that mean i = 10 - 2; is evaluated as 2 - 10 and then assigned to i ? which is incorrect. I am afraid, I am un-able to catch your point ("Right associativity" means to evaluate from right to left) Can you sight some examples depicting these which would make understanding clear ? Thanks Ravi [snip] Nov 13 '05 #11

 P: n/a "Ravi Uday" wrote: [snip] .... 2 unary plus + right 2 unary minus - right Does that mean i = 10 - 2; is evaluated as 2 - 10 and then assigned to i ? No, the '-' in "10 - 2" is a binary minus, not a unary minus. Unary means it has only one operand, binary means it has two operands. An example of unary minus is: i = - 2 * 2; The question here is whether the '-' or the '*' would be performed first. If you used the rule for binary minus, then '*' has higher precedence, but that's mathematically wrong. which is incorrect. I am afraid, I am un-able to catch your point ("Right associativity" means to evaluate from right to left) Can you sight some examples depicting these which would make understanding clear? Associativity for unary operators determines which side the compiler looks for the operand. For example, there are two '++' operators, one is left- associative and the other is right-associative. These are also known as prefix operators and postfix operators. Associativity for binary operators only really makes a difference when you have two of the same operator in sequence. Consider: 5 - 3 - 1 which is parsed as: (5 - 3) - 1 rather than: 5 - (3 - 1) Therefore the binary minus operator is left associative. Now compare to: i = j = 2; The right-associativity of the assignment operator means that it's equivalent to: i = (j = 2); instead of: (i = j) = 2; The latter would be absurd, as i would be set to the current value of j; the result of that expression is the previous value of j, which is not an lvalue, so it attempts to assign 2 to an rvalue. -- Simon. Nov 13 '05 #12

 P: n/a Ravi Uday wrote: -I have some confusion on the order in which the operators are evaluated. A statement such as 7/9*9+6-4 is evaluated in which order ? -Which of the following is evaluated first: a) && b) || c) ! Ravi.. There's a C operator precedence table available through the link below. -- Morris Dovey West Des Moines, Iowa USA C links at http://www.iedu.com/c Nov 13 '05 #13

 P: n/a On Wed, 24 Sep 2003 10:41:45 GMT Steve Zimmerman wrote: Ravi Uday wrote: > Hi, > > -I have some confusion on the order in which the operators are > evaluated. > A statement such as 7/9*9+6-4 is evaluated in which order ? > -Which of the following is evaluated first: > a) && > b) || > c) ! > > Thanks > Ravi > Thank you for these intelligent questions, Ravi. 7/9*9+6-4 is evaluated like this: 1. 7/9 = 0 2. 0*9 = 0 3. 0+6 = 6 4. 6-4 = 2 <--- Final answer is 2. The order in which the expressions are evaluated is not guaranteed, only the result and that each term is evaluated once (unless the compiler knows there are no side effects, which is the case here. So it might be evaluated like this: 1. 6-4 = 2 2. 7/9 = 0 3. 0*9 = 0 4. 0+2 = 2 <--- Final answer is 2. Or any one of a number of other possible orders, possible even evaluating some parts in parallel. This becomes important if some of the terms have side effects. Boolean shortcut is an exception. -- Mark Gordon Paid to be a Geek & a Senior Software Developer Although my email address says spamtrap, it is real and I read it. Nov 13 '05 #14

### This discussion thread is closed

Replies have been disabled for this discussion.