Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which has higher precedence in C -multiplication or division?

Tags:

c

#include <stdio.h>

int main(void) 
{
int w=2*3/2;             //1
int x=5%2*3/2;           //2
printf("%d  %d",w,x);
return 0; 
}

OUTPUT 3 1

In (1); it is giving higher precedence to multiplication than division while in (2) its vice versa. Why so?

like image 755
dee Avatar asked Feb 01 '14 17:02

dee


People also ask

Which has priority multiplication or division?

Division and multiplication, and addition and subtraction, have the same priority - the convention is to work from left to right when the order of operations would be unclear. Note: An alternative form of this mnemonic is BIDMAS, where the I stands for indices.

Does division take precedence over multiplication?

Order of operations tells you to perform multiplication and division first, working from left to right, before doing addition and subtraction. Continue to perform multiplication and division from left to right. Next, add and subtract from left to right.

Which operator has the highest precedence ()?

The logical-AND operator ( && ) has higher precedence than the logical-OR operator ( || ), so q && r is grouped as an operand. Since the logical operators guarantee evaluation of operands from left to right, q && r is evaluated before s-- .


1 Answers

They have the same precedence and are always executed left to right.

2*3/2 = (2*3)/2 => 3
            6/2 => 3 

and

5%2*3/2 = ((5%2)*3)/2 => 1
              (1*3)/2 => 1
                  3/2 => 1 (integer gets truncated)
like image 63
fede1024 Avatar answered Oct 14 '22 16:10

fede1024