#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?
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.
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.
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-- .
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With