DIGIT-ARC read-only archive · snapshot: July 23, 2026

precedence between pre-increment and multipliction. how it works

9 posts · originally from https://geek.digit.in/community/threads/precedence-between-pre-increment-and-multipliction-how-it-works.145234/

bijay_ps
bijay_ps Original post #1
Aug 17, 2011 at 7:14 PM
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..... :-( :-(
skeletor
Aug 18, 2011 at 7:11 AM
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 at 9:18 PM
Sorry my friend but I think each question has its own importance and worth.
I'm interested in knowing the answer only.....
A
Aug 19, 2011 at 5:57 PM
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 at 5:19 PM
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
G
gk2k #6
Aug 24, 2011 at 9:44 AM
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.
A
Aug 24, 2011 at 7:18 PM
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.
skeletor
Aug 24, 2011 at 7:36 PM
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. ;)
bijay_ps
Aug 29, 2011 at 9:42 AM
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