Thread: Post Increment an Pre Increment operators in c++

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    Post Increment an Pre Increment operators in c++

    Hi All,

    i have small problem in understanding the post and pre increments in c++. Could you please help me in understanding how exactly the equation is executed. Below is the code:
    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int main()
    {
    	int i=0,j=0;
    
    	i =2;
    	j =  ++i + i++ + i++ + i + ++i + i++;
    
    
    	cout << " \nThe value of j is : " << j;
    
    	return 0;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anil_
    Could you please help me in understanding how exactly the equation is executed.
    Yes. Read Stroustrup's answer to the FAQ: What's the value of i++ + i++? as it is applicable to your question.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by laserlight View Post
    Yes. Read Stroustrup's answer to the FAQ: What's the value of i++ + i++? as it is applicable to your question.
    Well the reference says, it is undefined. But how exactly the equation is executed? What is the value of "i" which is present in the middle of expression?

    I got the answers for evaluation of "i", through operators precedence, but it was only half way and i'm lost after that...

    my friends has derived some sort of assumed rules how the compilers gets the value and it applies correctly for all the permutational equations... but i need to know the exact procedure how it is executed.

    Please do help me.. if you want i can describe the answer i got which is only half the solution.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Well the reference says, it is undefined. But how exactly the equation is executed?
    it is undefined.
    That means that it is undefined, you can never be sure how the compiler will evaluate the expression.

    Jim

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by anil_ View Post
    my friends has derived some sort of assumed rules how the compilers gets the value and it applies correctly for all the permutational equations... but i need to know the exact procedure how it is executed.
    Well then you're out of luck because there is no exact procedure how it is executed. Different compilers will give different answers, and some may even do something totally bizarre.
    It's a trick question, there is no valid answer except that it's "undefined behaviour". Is it getting through yet?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pre & post increment need help
    By zing_foru in forum C Programming
    Replies: 6
    Last Post: 10-09-2009, 12:14 PM
  2. pre/Post Increment
    By ganesh bala in forum C Programming
    Replies: 1
    Last Post: 02-12-2009, 04:17 AM
  3. post vs pre increment
    By xddxogm3 in forum C Programming
    Replies: 13
    Last Post: 03-19-2004, 05:07 PM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. Pre-incrementoperators and post-increment operators
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 12-26-2001, 06:25 PM