Bit of background to explain the reasoning: I’ve been programming for a good while, but took a break between 2012 and 2014 for other stuff. Before that break, I would hardly ever hear about “++i”, let alone hear decent explanations about it (“…isn’t it… just better?” was common to hear). Now, however, I see it everywhere, and even some text editors seem to use this by default (Sublime Text comes to mind).
So I’ve been wondering; did “++i” become suddenly very popular as of late, or is my new work environment pro-prefix increment operators?
(another possible solution is that my old environment wasn’t big on “++i” for some reason)
Note that I am definitely not asking whether this is a good thing or not; I’m well aware of how pre-increment operators work, and honestly there are enough debates about this on SO. I’m just asking if this is a local trend, or a more global one. Also this is strictly for C, not C# or Java.
8
In C++, the pre-increment operator may be written to return a reference to the incremented object. The post-increment operator has to return a copy (because the return value is the value before the increment operation).
So, if you’re using increment and don’t care about the return value (such as in a loop increment), then you want to prefer pre-increment as you might be incrementing something that’s costly to copy.
In C this is irrelevant, but I know that once I developed the habit in C++, it spread to my C code as well.
11