I have the following code:
#include<stdio.h> void main(){ int x; x=1%9*4/5+8*3/9%2-9; printf("%d \n", x); } The output of the program is -9. When I tried to breakdown the code according to operator precedence(* / %,Multiplication/division/modulus,left-to-right) answer turns out to be -8.
Below is the breakdown of the code:
x=1%9*4/5+8*3/9%2-9; x=1%36/5+24/9%2-9; x=1%7+2%2-9; x=1+0-9; x=-8; Can someone explain how the output is -9.
1%9*4/5+8*3/9%2-9to be(1%9*4/5)+(8*3/9%2)-9. Multiplication, division and modulo operators all have same operator precedence and left-to-right associativity.void mainis wrong.mainshould returnint.1 % 9 * 4 / 5is evaluated left-to-right; same precedence, so it is((1 % 9) * 4) / 5.