Operator Order

R

Ravi Uday

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
 
N

Nils Petter Vaskinn

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.
 
S

Simon Biber

Ravi Uday said:
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.
 
S

Slartibartfast

Ravi Uday said:
-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.
 
S

Steve Zimmerman

Ravi said:
> 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
 
M

Minti

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"
 
D

Dan Pop

In said:
Hi, [operator precedence question]
Ravi

That 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 operator
precedence rules, far less chance of making a mistake, far less chance of
someone 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
 
K

Keith Thompson

-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.)
 
R

Ravi Uday

[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]
 
S

Simon Biber

Ravi Uday said:
[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.
 
M

Morris Dovey

Ravi said:
-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.
 
M

Mark Gordon

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.

<snip>

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,082
Messages
2,570,586
Members
47,209
Latest member
Ingeborg61

Latest Threads

Top