DISCLAIMER: This is not a real-world example. It is just a theoretical question of how these languages work.
What exactly are the differences between C/C++, C#, and Java when it comes to post & pre increment operators?
This is what I get with VC++10, Java 1.6, and C# 4
int a = 2; int b = a++ + a++; int c = ++a + a++ + a++; +-----+------+------+----+ | C | C++ | Java | C# | +-----+-----+------+------+----+ | a | 7 | 7 | 7 | 7 | +-----+-----+------+------+----+ | b | 4 | 4 | 5 | 5 | +-----+-----+------+------+----+ | c | 15 | 15 | 16 | 16 | +-----+-----+------+------+----+
a prefix occurring originally in loanwords from Latin, where it meant “before” (preclude; prevent); applied freely as a prefix, with the meanings “prior to,” “in advance of,” “early,” “beforehand,” “before,” “in front of,” and with other figurative meanings (preschool; prewar; prepay; preoral; prefrontal).
The West Germanic or Anglo-Frisian dialect from which English developed; (also occasionally) English before written records. Also more generally: any language which may be regarded as a forerunner of (modern) English.
The prefix pre-, which means “before,” appears in numerous English vocabulary words, for example: predict, prevent, and prefix! An easy way to remember that the prefix pre- means “before” is through the word prevent, for when you come “before” something else to stop it from happening, you prevent it.
Pre- is used to form words that indicate that something takes place before a particular date, period, or event. ... his pre-war job.
Java and C# evaluate expressions from left to right, and the side-effects are visible immediately.
In C++, the order of evaluation of subexpressions is unspecified, and modifying the same object twice without an intervening sequence point is undefined behavior.
I don't have the time to write up a detailed description of the differences between C++, C, C# and Java. I will merely say that the C# behaviour of the pre and post increment operators is fully specified (in single-threaded scenarios; if you want to know about its atomicity, guarantees about observations of read and write orders in multi-processor weak memory models and so on, you're on your own to do that research.) It is not fully specified in C and C++; a compiler has broad lattitude to do whatever it pleases with re-ordering side effects. I have never used Java so I'm not going to hazard a guess as to what Java does.
For more information on what C# does you should read the C# specification. For a short take on it, read my answer to this question:
What is the difference between i++ and ++i?
For an even shorter take:
Subexpressions in a C# expression are logically grouped by precedence and associativity, and then evaluated from left to right regardless. (So for example, A() + B() * C() evaluates A(), then B(), then C(). The fact that the multiplication "comes before" the addition is irrelevant; the subexpressions are always evaluated left to right.)
If the evaluation of a subexpression causes a side effect because of a pre or post increment subexpression then the side effect happens immediately before the result is produced.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With