Fundamental C- Side Effects, Sequence Points And Lazy Evaluation
Written by Harry Fairhead   
Monday, 11 February 2019
Article Index
Fundamental C- Side Effects, Sequence Points And Lazy Evaluation
Dangerous Expressions
Legal Expressions

So far sequence points simply tell you when you can expect side effects to be complete, but they don’t help with the problem of:


The sequence point is at the end of the expression and it still isn’t clear whether the side effects occurred as assignment then increment, or increment then assignment. To deal with this the C11 standard also introduced two conditions that have to be satisfied to make an expression legal:

Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.

These two rules make i=i++ undefined as the variable has its value changed more than once. The second condition also makes expressions such as:


undefined as, even though i is only modified once, its original value is used to determine where the result is stored and not just what the result is.

Many programmers find the C sequence rules difficult to understand and apply, but this really isn’t the problem. If your code causes you to contemplate the role of a sequence point or the conditions that apply then you are probably writing expressions that should not be written.

It really shouldn’t need a rule to convince you that writing:


is a bad idea. In general, relying on side effects isn’t a good idea unless their role and meaning is very simple and very clear. If an expression has you wondering exactly what it means then it will make you or some other programmer wonder what it means the next time you see it, even if you manage to work out its meaning this time around.

Summary Of Chapter

  • C has only two basic types of data – integer and floating-point.

  • The amount of memory that a data type takes is machine-dependent.

  • Variables have to be declared before use and optionally initialized.

  • All integer types come in signed and unsigned forms.

  • Unsigned overflow is always defined as roll over.

  • Signed overflow is an example of undefined behavior.

  • It is a good idea to specify the type of any literal you are using.

  • You can use integer types that have a well defined size via the stdint.h library introduced in C99.

  • Variables can be declared as const to make sure they cannot be changed and volatile to make sure that the compiler cannot assume that they do not change.

  • How long a variable exists and where you can use it – lifetime and scope – are important ideas but they only make sense when you have understood functions in the next chapter.

  • C makes use of the idea of an lvalue, something that has an address, and an rvalue, something that doesn’t.

  • Expressions form a sophisticated programming language in their own right consisting of operators with precedence and associativity rules.

  • Sequence points determine when the side effects of an expression are determined and sequence point rules make some expressions undefined.

  • Logical OR and AND are lazy evaluated which means that the right-hand expression may never be evaluated and hence may never cause any side effects.

  • If you find you are having to work out sequence points and rules the chances are you are writing obscure code.




  • Harry Fairhead is the author of Raspberry Pi IoT in C (I/O Press). This extract is from his recent book where he takes an in-depth look at C for use in any close-to-the-hardware context.


Related Articles

Remote C/C++ Development With NetBeans

Raspberry Pi And The IoT In C

Getting Started With C/C++ On The Micro:bit

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.


Apache Lucene Adds Similarity Vector Searches

Apache Lucene 9.10 has been released with support for similarity-based vector searches. Other improvements include block join compatible index sorting, and several improvements to ensure the software  [ ... ]

Sinusoidal Tetris

This is just for fun and education, but it suggests that there might be other games waiting to be thought up along the same lines.

More News

raspberry pi books



or email your comment to:

Last Updated ( Sunday, 17 March 2019 )