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?

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.
If this question can be reworded to fit the rules in the help center, please edit the question.

22

(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)
–
Michael StumNov 1 '10 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 ?!
–
WildlingMay 27 '11 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++?
–
Keith ThompsonJul 30 '13 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.

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.
–
nlawalkerMay 27 '11 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.
–
GiorgioSep 22 '14 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.

+1. Knowing the difference between the pre- and post-fix increment/decrement operators is worth it for situations like this.
–
MaulrusNov 2 '10 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.
–
Michael StumNov 2 '10 at 2:10

8

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.
–
David ThornleyNov 2 '10 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".
–
rmacMay 27 '11 at 19:39

6

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)
–
Bart van HeukelomDec 6 '11 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.
–
Steve314May 28 '11 at 3:57

+1: This actually answers asked question (that is, you are not reading between the lines).
–
Thomas EdingJul 30 '13 at 22:02

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.

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.

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.
–
Matt McNabbMay 21 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?
–
Walt StoneburnerMay 26 at 20:29

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.
–
DeadMGJul 16 '11 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.
–
Mike DunlaveyJul 16 '11 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".
–
Keith ThompsonJul 30 '13 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!

Yet this doesn't adress the actual question: why do C developers ask about this more than others. The OP understands the operators.
–
Martijn PietersSep 22 '14 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.
–
Matt McNabbMay 26 at 21:28