-1
int a=98, b=10;
float c;
c=a/b;

Output: c=9

I know because of implicit type conversion, c value is 9 not 9.8 but then I encountered this question:

int a,b,c,d;
a=40;
b=35;
c=20;
d=10;
printf("%d",a*b/c-d);

output: 60

Now if we see the precedence for equation is right to left and according to BODMAS rule b/c(35/20) will be performed first so 35/20 = 1.75 and then implicit conversion to integer make it to 1 and then rest will follow up the answer must be 30 but the output is 60 which is correct answer. Can you explain me why?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
DIGIT
  • 79
  • 2
  • 11

2 Answers2

1

Operator precedence in C for * and / in C is left-to-right: http://en.cppreference.com/w/c/language/operator_precedence

So a*b will be done first. It's equivalent to writing ((a*b)/c)-d.

AntonH
  • 6,359
  • 2
  • 30
  • 40
0

So your execution will follow left to right execution for / and *:

((40 * 35) / 20) - 10 = ((1400) / 20) - 10 = (70) - 10 = 60
unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40