Created
May 6, 2013 21:15
-
-
Save nathania/5528266 to your computer and use it in GitHub Desktop.
Pre- and post-increment operators in C++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::cout, std::endl; | |
int x = 0; | |
int pre = ++x; // ++x incremements x, and returns (the incremented value of) x | |
cout << pre << ', ' << x << endl; // 1, 1 | |
int post = x++; // x++ returns (the original value of) x, and incremements x | |
cout << post << ', ' << x << endl; // 1, 2 | |
/* | |
http://stackoverflow.com/questions/1812990/incrementing-in-c-when-to-use-x-or-x | |
http://stackoverflow.com/questions/4445706/post-increment-and-pre-increment-concept | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment