Side effects in C

c, c++, side-effects

Solution

You can use an assignment expression as a value:

double d = 3.5;

int x, y;

printf("%d", x = d); // Prints "3".

y = (x = d) * 5; // Sets y to 15.

double z = x = d; // Sets z to 3 (not 3.5).

The value produced by `x = d`, is its main effect. The changing of the value of `x` is a side effect.

Problem

I thought that my understanding of side effects in programming languages was OK. I think this is a great definition from wikipedia: "in addition to returning a value, it also modifies some state or has an observable interaction with calling functions or the outside world." However, I read this in the same link(yes, I know that is probably not the best place to look for examples): "One common demonstration of side effect behavior is that of the assignment operator in C++. For example, assignment returns the right operand and has the side effect of assigning that value to a variable. This allows for syntactically clean multiple assignment:" ``` int i, j; i = j = 3; ``` Why do they consider that a side-effect? It is the same as two simple assignment statements to 2 local variables. Thanks in advance.

Original source