CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: postfix

Threaded View

  1. #1
    Join Date
    Sep 2008
    Posts
    1

    postfix

    Hi,

    Could someone please explain how the following ends up outputting 0?

    Code:
    #include <iostream>
    int main()
    {
    	int a=0; 
    	std::cout << a++ + a++ << std::endl;
    	return (0); 
    }
    I interpret this as follows.
    Compiler reads from right to left. So first the a++ on the right gets processed. Since its postfix, operator + gets 0. and then a is set to 1. Now the a++ on the left gets processed. a=1 is passed to +, and then a is incremented by 1 to 2. So the final result should be 1.

    cheers,

    pooya
    Last edited by pooya; September 20th, 2008 at 08:51 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured