# precedence between pre-increment and multipliction. how it works



## bijay_ps (Aug 17, 2011)

I wrote this code
	
	



```
#include<stdio.h>
void main()
{
    int i,j;
    i=3;
    j=++i*++i*++i;
    printf("%d\n",j);
}
```
 I am getting 216 as output in windows compiler and 150 in ubuntu compiler,but I anticipated that it should be 120.
So guys plz tell me how this code is working?? and one more thing this ++ oprtr has right to left associativity and * has left to right associativity.

Hey friends I figured it out,how its showing that result in windows (and when I say windows,I actually mean turbo-c compiler). But still can't understand how its showing 150 in ubuntu...... 'coz C lang. is same and concept cannot differ from OS to OS,only syntax may differ a little. So plz help me out here.....


----------



## ico (Aug 18, 2011)

This behaviour can vary from compiler to compiler. I don't know why Yindan teachers and books by incompetent writers like Yashwant Kanetkar and Sumita Arora ask such questions.


----------



## bijay_ps (Aug 18, 2011)

Sorry my friend but I think each question has its own importance and worth. 
I'm interested in knowing the answer only.....


----------



## abhijangda (Aug 19, 2011)

First thing stop using Turbo C or Turbo C   as your Windows compiler, instead use GNU C Compiler, Microsoft Visual C Compiler.
Ubuntu uses GNU C Compiler (gcc) which is an updated compiler supporting ANSI C while Turbo C is decades old and doesn't support ANSI C rather it supports old C (i think K&R C). As ANSI C is somewhat different from old C, so you are getting different answers.
Stop reading books like Let us C etc. These are out-dated ones. Read books which supports ANSI C like Programing in ANSI C by Denis M. Ritchie.


----------



## bijay_ps (Aug 23, 2011)

I don't know why you guys are thinking that I am reading Let Us C....... FYI I am following Complete refrence....... and I came across this question 'coz one of my frnd asked me this


----------



## gk2k (Aug 24, 2011)

According to the C standard the behavior when increment a variable and using it in within a single sequence point is undefined and is dependent on the individual compiler implementation.

Turbo C all the three increments are performed before the multiplication operation i.e j=6*6*6

In gcc the expression is split as j=(++i*++i)*++i. i.e a sequence point is inserted before the second multiplication so the resultant operation is j=(5*5)*6. Here two increments are performed before the first multiplication so i=5.


----------



## abhijangda (Aug 24, 2011)

bijay_ps said:


> I don't know why you guys are thinking that I am reading Let Us C....... FYI I am following Complete refrence....... and I came across this question 'coz one of my frnd asked me this


because Let us C follows Turbo C.


----------



## ico (Aug 24, 2011)

bijay_ps said:


> Sorry my friend but I think each question has its own importance and worth.
> *I'm interested in knowing the answer only.....*


And I've actually given you the answer. This is a compiler specific behaviour. The answer will vary in each compiler you use.

@abhijangda: He might not be following 'Let Us C'  that was just my random rant against Yindan teacher/authors who as pre and post increment questions.


----------



## bijay_ps (Aug 29, 2011)

gk2k said:


> According to the C standard the behavior when increment a variable and using it in within a single sequence point is undefined and is dependent on the individual compiler implementation.
> 
> Turbo C all the three increments are performed before the multiplication operation i.e j=6*6*6
> 
> In gcc the expression is split as j=(++i*++i)*++i. i.e a sequence point is inserted before the second multiplication so the resultant operation is j=(5*5)*6. Here two increments are performed before the first multiplication so i=5.



Thanks for beautiful explanation now I understood the concept fully........ thnx once again



ico said:


> And I've actually given you the answer. This is a compiler specific behaviour. The answer will vary in each compiler you use.
> 
> @abhijangda: He might not be following 'Let Us C'  that was just my random rant against Yindan teacher/authors who as pre and post increment questions.



thnx for Ur reply too...... now I got the concept


----------

