Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pre & post increment operator behavior in C, C++, Java, & C# [duplicate]

Tags:

java

c++

c

c#

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 | +-----+-----+------+------+----+ 
like image 915
Nick Avatar asked Jun 23 '11 16:06

Nick


People also ask

Why does pre mean?

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).

What is pre English?

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.

What is pre and examples?

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.

How do you use pre?

Pre- is used to form words that indicate that something takes place before a particular date, period, or event. ... his pre-war job.


2 Answers

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.

like image 72
fredoverflow Avatar answered Sep 28 '22 09:09

fredoverflow


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.

like image 29
Eric Lippert Avatar answered Sep 28 '22 08:09

Eric Lippert