dee dee - 1 month ago 15
C Question

Which has higher precedence in C -multiplication or division?

#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?

Answer

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)
Comments