precedence between pre-increment and multipliction. how it works

bijay_ps

Broken In
I wrote this code
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

Super Moderator
Staff member
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.
 
OP
bijay_ps

bijay_ps

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

abhijangda

Padawan
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.
 
OP
bijay_ps

bijay_ps

Broken In
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

gkbhat.blogspot.com
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.
 

ico

Super Moderator
Staff member
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' :p that was just my random rant against Yindan teacher/authors who as pre and post increment questions. ;)
 
OP
bijay_ps

bijay_ps

Broken In
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

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' :p 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
 
Top Bottom