Just a random observation, it seems that on StackOverflow.com, there are questions about if "++i == i++". That question gets asked all the time though, I think I saw it asked about 6 or 7 times in the past 2 months.

I just wonder why C developers are so interested in it? The same concept/question exists for C# and Java devs as well, but I think I saw only one C# related question.

Is it because so many examples use ++i? Is it because there is some popular book or tutorial? Is it because C developers just love to cram as much as possible into a single line for 'efficiency'/'performance' and therefore encounter 'weird' constructs using the ++ operator more often?

  • 23
    (For me as a C# dev, i++ and ++i are equal. I know they are not, but if I write code that depends on the difference I would rather refactor it to be more readable and expect the compiler and JITter to take care of it, but as a C# dev I'm not trying to save one or two clock cycles because I'm relatively inefficient anyway already) Nov 1, 2010 at 23:37
  • 1
    as budding programmers we love playing with arithmetic, logic and are curious how things work. I think thats a big reason we noobs love such questions. it might not be a pathbreaking feature but it sure is interesting! isnt that the reason we became programmers in the first place ?!
    – Chani
    May 27, 2011 at 5:27
  • 2
    Are you literally referring to the expression ++i == i++, or more generally about the difference in meaning between ++i and i++? Jul 30, 2013 at 22:39

10 Answers 10


I suspect that at least part of it is a bit simpler: even now, we see a lot of questions like this starting around the beginning of the school year, and they gradually taper off throughout the year.

As such, I think it's fair to guess that quite a few of them are simply a result of classes in which the teacher talks at least a little about it, but doesn't explain his point(s) very well (as often as not because he doesn't really understand them himself). Especially based on the people who seem to ask these questions, few are based on actual coding.

  • 14
    Isn't this the opposite of Eternal September? Eternal September was when AOL started offering Usenet access to their customers, making it seem like September (the month when new students beginning school traditionally bombarded the boards as newbies) year-round.
    – nlawalker
    May 27, 2011 at 14:10
  • It is an interesting theory but maybe it is not always the teachers' fault if someone did not understand the material: maybe the students were (1) not paying enough attention during class and (2) too lazy to look for an answer on stackoverflow before asking the same question again.
    – Giorgio
    Sep 22, 2014 at 11:51

Because C programmers HAVE to understand order of operations. C# developers don't necessarily use the bitwise operators (&, |, ~) or prefix operators since we're normally more worried about other stuff. However, what we C# devs don't realize is that we should know what the operators we use on a daily basis do and how to use them properly. Most of us just avoid the places where this may be a problem.

Off the top of your head, what is the output of this snippet?

    double x = 5.5;
    x = 5.5;

I would say a good number of seasoned C# devs have no idea what the output to the Console is.

  • 3
    +1. Knowing the difference between the pre- and post-fix increment/decrement operators is worth it for situations like this.
    – Maulrus
    Nov 2, 2010 at 1:28
  • 4
    I kinda see the point and I wouldn't know the output either (I believe it's 5.5 and 6.5, but I'm not sure), and for me it wouldn't matter as I would refactor it to x++; Console.WriteLine(x); immediately. I'm not totally ignorant however, I just think it's a big code smell with zero usability. Nov 2, 2010 at 2:10
  • 9
    The answer to the question has nothing to do with order of operations, or the difference between prefix and postfix increments. It's a matter of defined and undefined behavior. Nov 2, 2010 at 15:11
  • 2
    I encountered this today. Something like if (myList[i++] == item). Clever. I found it looking for the source of an IndexOutOfRange exception... Indeed, very "clever".
    – rmac
    May 27, 2011 at 19:39
  • 7
    I think it's 5.5 and 6.5, but I've never seen ++ used on a float before, and I must say it doesn't feel very appropriate. Do people do that? (I'd prefer += 1) Dec 6, 2011 at 20:48

I think this is obvious. In C#, for an int i, i++ == ++i is always false while ++i == i++ is always true, reliably, and everyone who’s interested can find this out easily just by learning the rules of C# (or indeed by just running it).

In C and C++, on the other hand, it’s close to undefined because it depends on the compiler, the execution environment, the platform, etc., so it’s a much harder question to answer, so lots of people ask it.

  • +1 - but it's not really a harder question to answer. "undefined" is one word. All the "it depends on ..." stuff is stuff that most people don't worry about. Even if you're only writing for one particular platform, you should be using the normal idioms for the language as a whole, and most uses of pre-increment and post-increment operators are part of a few common idioms.
    – user8709
    May 28, 2011 at 3:57
  • +1: This actually answers asked question (that is, you are not reading between the lines). Jul 30, 2013 at 22:02

It's a popular question because it's a trick question. It's undefined.

The link above goes to a FAQ on Bjarnes Stroustrup's homepage, which addresses this question specifically, and explains why this construct is undefined. What it says is:

Basically, in C and C++, if you read a variable twice in an expression where you also write it, the result is undefined.

Having the order of evaluation undefined is claimed to yield better performing code.


Most likely because in C, it really matters if you write i++ or ++i when you're doing pointer arithmetic. There, increasing i before or after an operation could be crucial. I'd give you an example but the last time I wrote C was 10 years ago...

In C#, I've never come across a situation that would require me to think about i++ or ++i because AFAIK, for the for/while loops, it doesn't really matter.

  • 3
    it's still important to understand the differences in C# -- just because you seem to only use them in for/each loops doesn't mean that's the only place you'll find them
    – STW
    Nov 2, 2010 at 0:01
  • totally true that. Nov 2, 2010 at 0:25
  • 3
    This isn't relevant to the question. The question isn't about the difference between i++ and ++i, but combining them in the same expression. Nov 2, 2010 at 15:12
  • @David The question is why C programmers are curious about it. The answer (It matters in C, not so much in C#) is completely relevant".
    – Florian F
    Sep 22, 2014 at 7:57

A few years ago I read that these operators were considered dangerous so I tried to dig more info on it. If stackoverflow had been up at that time, I would have asked it there.

Now it is because some twisted people write loops such as

while( [something] )
  a[++j] = ++j;

Even now, I'm not too sure about what will/should happen, but it's pretty clear to me that multiple ++[var] on the same line is asking for trouble.

  • 2
    Increments have undefined behavior as part of an assignment in C
    – tzenes
    Nov 2, 2010 at 6:16
  • 7
    Assignments like x = ++j; are well defined in C. The above is undefined because j is modified twice without an intervening sequence points. Dec 30, 2010 at 18:50

Two reasons.

The correct implementation of ++i is to increment i and then return it. The correct implementation of i++ is to save the current value, increment i, and return the saved value. Knowing these are not implemented as synonymns is important.

The question then becomes when does the compiler apply them while evaluating an expression, such as equality.

If the equality test is done first, then the pre- and post- increment operators, you're going to have to write different logic than if the left hand side (lhs) and the right hand side (rhs) are evaluated first, then the equality.

It's all about order of operations, and that in turn affects the code one writes. And, not too surprisingly, not all languages are in agreement.

Basic tests list this allow developers to test to see if their assumptions are correct, as well as if the actual implementation matches the language specification.

This can have all kinds of impact when working with pointers, loops, or returning values.

  • 5
    Basic tests on this will be misleading, because it's undefined behavior. If it works precisely as you expect this time, there's no guarantee it will next time. Nov 2, 2010 at 17:40
  • You've answered the wrong question. You should answer why C programmers are curious about it.
    – Hugo
    Jul 17, 2011 at 7:32
  • Your second para is incorrect anyway (prior to C++11, at least): the behaviour of ++i is to use the value i+1 and at some point in time, perhaps before or after that, but before the next sequence point, increment i.
    – M.M
    May 21, 2015 at 22:28
  • @MattMcNabb - Can you point me to a specification for that, I'd be curious to learn if things have changed in implementation. Back in the days where C mapped nicely to PDP assembly, the ++i injected an INC instruction, whereas the suffix version had to copy the value in a register for use an instruction after the increment. What constituted the necessity for the change you described? May 26, 2015 at 20:29

I think it's a culture shock thing.

As already pointed out, the behaviour of an expression in C or C++ can be undefined because the order of execution of the post-increments etc isn't strictly defined. Undefined behaviour is pretty common in C, and of course a lot of that was imported into C++.

To someone who has done some programming in another language - someone who has picked up the idea that programming languages are supposed to be precise, and that computers are supposed to do what they're told to do... well, it's a shock to discover that C is intentionally vague about some things.

  • C is this way because it was designed to support vastly more platforms than even existed when C# was defined, including the strangest embedded or mainframe platforms you can think of. C#, on the other hand, runs on ... x86, and 360? maybe ARM too? Nothing else. That's why C# can define a lot more.
    – DeadMG
    Jul 16, 2011 at 20:49
  • ++ Also, wasn't C first built on a PDP-11? Where there were auto-increment instructions? C wanted to be "close to the machine" so it could make useful use of the instruction set. Jul 16, 2011 at 22:31
  • @MikeDunlavey: No, C's ++ and -- operators were not based on the PDP-11, which did not exist when those operators were introduced in C's predecessor language B. See cm.bell-labs.com/who/dmr/chist.html, and search for "auto-increment". Jul 30, 2013 at 22:37

Perhaps it's just that C developers use ++ incrementation more often in general -- seeing as how C has no "foreach" or similar statement to iterate through arrays. Oh, and of course pointer arithmatic, as previously mentioned!

  • 1
    Good point, forgot that foreach is a relatively modern concept. Jul 17, 2011 at 1:52

The difference between both parts : the post and preincrement are working the way these two pieces of code should function in every language. THIS IS NOT BE LANGUAGE DEPENDENT!!!


This is pre increment. This piece of code will first increase the value of i before it sends the value to the line where it is used.


This is the post increment. This piece of code will return the value of i before it increases the value of i on the line where it is used.

In another small example :

int main(void)
  int i = 0; 
  int result; 
  result = 10 + ++i; 
  printf("%d \n", result); // = 11 

  int j = 0; 
  result = 10 + j++; 
  printf("%d \n", result);// = 10
  return 0;

This code compiling with results on codepad.

This has to do with the internal implementation of the operator overloading.

++i increases directly the value (and is therefore a little bit faster)

i++ first creates a copy that is returned to the place where needed and afterwards the original value of the i variable is increased by one.

I hope this is clear now.

  • Yet this doesn't adress the actual question: why do C developers ask about this more than others. The OP understands the operators. Sep 22, 2014 at 6:43
  • Also this answer is factually incorrect. ++i is not faster, and i++ does not create a copy. Further, ++i behaves slightly differently in C than in C++, let alone between other languages.
    – M.M
    May 26, 2015 at 21:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.