What are side effects (computing)? Ever wondered about i++ and ++i output when in printfs. I know they suck !

Side Effects to me came into existence when interviewer asked me "What will be the output of

printf("%d",i++ + ++i + i++ + ++i ...) ; // This goes to infinity :'(

when i=5 and we continuously argued on the concepts, and the result was both of us were not aware of the concept of side effects and sequence point.

Many statements can have a side effects so be careful before using a functionality or writing a line of code. A function or an expression return something when evaluated, but sometimes instead of just returning a value, functions/expressions may modify a value ( global or static variable) like in above question each expression modifies the value of i, instead of just returning something. Jumping back to above example, the line of code is said to have Side Effects and this is bad.

Though Side effects are essential to enable a program to interact with the outside world (people, filesystems, other computers on networks), we should only write line of code which is predictable.

We will learn below how to use side effects effectively.

Plain old definition of side effect goes as:

A function or expression is said to have a side effect if, in addition to returning a value, it also modifies some state or has an observable interaction with calling functions of the outside world. For example, a function might modify a global or static variable, modify one of its arguments, raise an exception, write data to a display or file, read data, or call other side-effecting functions. In the presence of side effects, a program’s behavior depends on history; that is, the order of evaluation matters. Understanding and debugging a function with side effects requires knowledge about the context and its possible histories

In C Language printf, post/pre increment/decrement ++, -- have side effects. It doesn’t mean you shouldn’t use them but read below how to use them effectively so you don’t run into coding trouble and smash your head against the wall, to get the correct output of ugly expressions like above one. The best way to deal with side effects is Sequence points.

By definition:

A sequence point defines any point in a computer program’s execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed. They are often mentioned in reference to C and C++, because the result of some expressions can depend on the order of evaluation of their subexpressions. Adding one or more sequence points is one method of ensuring a consistent result, because this restricts the possible orders of evaluation.

Did you understood what they are trying to say ? No.

Don’t worry sequence points are not code/expressions you will need to put in your program but is a programming standard which asks you to evaluate all the side effects of previous expression (like i++ or ++i) before continuing further execution of program. An Example should do the trick.


int main() { 
int apple=5,window=2,linux=7; // Yes I am bit biased.
apple++; // wild Side effect appears
int samsung = ++windows + apple;  // Another wild side effect
linux--; //  Linux comes with side effect, Oh yes
}

In above code we have 3 expressions that have side effects, now the how you define sequence points ??

The first sequence point occurs above samsung statement, where apple side effect is already evaluated, second above linux. Guess the third one where it should occur.

You should always follow above style of coding with sequence points and avoid i++ + ++i kind of expressions as you can’t get sequence points here. Tell me in comments.

Wondering what was the answer to above question at starting of post, The answer was its UNPREDICTABLE. YES its something how compiler evaluates your expression and may vary in different systems.

DISCLAIMER : if you think this article can be improved or any other suggestions , please leave a comment.