Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 17-08-2011, 07:14 PM   #1 (permalink)
Right Off the Assembly Line
 
bijay_ps's Avatar
 
Join Date: Mar 2011
Location: chennai
Posts: 44
Default precedence between pre-increment and multipliction. how it works


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.....
__________________
Follow 7H3 G33K !nside YOU
bijay_ps is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 18-08-2011, 07:11 AM   #2 (permalink)
ico
.
 
ico's Avatar
 
Join Date: Jun 2007
Location: New Delhi
Posts: 8,944
Default Re: precedence between pre-increment and multipliction. how it works

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.
ico is offline   Reply With Quote
Old 18-08-2011, 09:18 PM   #3 (permalink)
Right Off the Assembly Line
 
bijay_ps's Avatar
 
Join Date: Mar 2011
Location: chennai
Posts: 44
Default Re: precedence between pre-increment and multipliction. how it works

Sorry my friend but I think each question has its own importance and worth.
I'm interested in knowing the answer only.....
__________________
Follow 7H3 G33K !nside YOU
bijay_ps is offline   Reply With Quote
Old 19-08-2011, 05:57 PM   #4 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: precedence between pre-increment and multipliction. how it works

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.
abhijangda is online now   Reply With Quote
Old 23-08-2011, 05:19 PM   #5 (permalink)
Right Off the Assembly Line
 
bijay_ps's Avatar
 
Join Date: Mar 2011
Location: chennai
Posts: 44
Default Re: precedence between pre-increment and multipliction. how it works

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
__________________
Follow 7H3 G33K !nside YOU
bijay_ps is offline   Reply With Quote
Old 24-08-2011, 09:44 AM   #6 (permalink)
gkbhat.blogspot.com
 
Join Date: Apr 2008
Location: Mangalore/Bangalore
Posts: 103
Default Re: precedence between pre-increment and multipliction. how it works

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.
__________________
blogging at http://gkbhat.blogspot.com
gk2k is offline   Reply With Quote
Old 24-08-2011, 07:18 PM   #7 (permalink)
Alpha Geek
 
Join Date: Jan 2007
Location: In your hearts
Posts: 828
Default Re: precedence between pre-increment and multipliction. how it works

Quote:
Originally Posted by bijay_ps View Post
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.
abhijangda is online now   Reply With Quote
Old 24-08-2011, 07:36 PM   #8 (permalink)
ico
.
 
ico's Avatar
 
Join Date: Jun 2007
Location: New Delhi
Posts: 8,944
Default Re: precedence between pre-increment and multipliction. how it works

Quote:
Originally Posted by bijay_ps View Post
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.
__________________
.
ico is offline   Reply With Quote
Old 29-08-2011, 09:42 AM   #9 (permalink)
Right Off the Assembly Line
 
bijay_ps's Avatar
 
Join Date: Mar 2011
Location: chennai
Posts: 44
Default Re: precedence between pre-increment and multipliction. how it works

Quote:
Originally Posted by gk2k View Post
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

Quote:
Originally Posted by ico View Post
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
__________________
Follow 7H3 G33K !nside YOU
bijay_ps is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:33 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2