Skip to content

Instantly share code, notes, and snippets.

@nathania
Created May 6, 2013 21:15
Show Gist options
  • Save nathania/5528266 to your computer and use it in GitHub Desktop.
Save nathania/5528266 to your computer and use it in GitHub Desktop.
Pre- and post-increment operators in C++.
#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