Post Increment an Pre Increment operators in c++

This is a discussion on Post Increment an Pre Increment operators in c++ within the C++ Programming forums, part of the General Programming Boards category; Hi All, i have small problem in understanding the post and pre increments in c++. Could you please help me ...

  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
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    2,917
    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,308
    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, 01: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, 04: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21