Home » Sequence Points in C

Sequence Points in C

by Online Tutorials Library

Sequence Points in C

In this article, we will learn what are Sequence Points in C Programming Language are. Furthermore, to understand them more easily, we will also discuss some examples of sequence points along with their practical implementation.

Introduction

In general, we can consider the sequence point as it defines any point in the execution of a computer program at which it guarantees or ensures that all the side effects of the previous evaluation of the program’s code are done or successfully performed. However, it also ensures that none of the alterations or side effects of the subsequent evaluations is yet performed at all. In other words, we can say that in imperative programming, a sequence point defines any point in the execution of a computer program at which it is guaranteed that all the side effects of the previous evaluation would have been done. In addition, No side effects from the subsequent evaluation have been performed yet. It’s quite possible that the word “side effect” may be confusing to you here. However, if it does, then let us see what exactly is the “side effect” is.

Side Effect

The term “Side Effect” is nothing but the side effect or changes done by any sort of function or expression where the state of something gets changes.

To understand the sequence point more detail, you can consider the following given examples:

Example 1

The output of the above-given program is undefined or non-predictive because the output of the above program will differ on the different machine or compiler because it is like asking for the value of an undefined automatic variable. The main reason behind the undefined behaviour of the above program is that the “+” operator does not have any defined standard order of evaluation for its operand. Therefore, we can not predict which function “f1” or “f2” will execute first. However, several other operators are available similar to the “+” operator such as ‘-‘, ‘/’, ‘*’, Bitwise AND &, Bitwise OR |, .. etc.

Example 2

The output of the above program is also undefined because even evaluation of the expression can also cause side effects. For example, in the above program, the final value of p is ambiguous because it totally depends on the order of expression evaluation. Therefore, if the function “f1();” executes first, the value of p will be 55; otherwise 40.

Sequence points

These are some of the basic sequence points available in C Language

  1. logical AND &&
  2. logical OR ||
  3. conditional ?
  4. comma ,

1. logical AND &&

In case of the logical AND && operator, its left operand will be completly executed first, and it’s all side effects before continuing. However, if the left operand evaluates to false, the execution process will stop, and the other operand will not execute at all.

2. logical OR ||

In the case of logical OR ||, its left operand has to be completely executed first and all its side effects before continuing. However, if the left operand evaluates to false (or nonzero), the other operand would not be executed at all.

3. Conditional?

In the case of the conditional operator, The first operand of the conditional operator will be evaluated first, and all its side effects are completed before continuing.

4. comma,

The left operand of the comma operator has to be completely evaluated first, and all its side effects have to be taken place before continuing. However, both operands of the comma operator are always evaluated.

Note: The comma operator in a function call does not guarantee the order of evaluation.


Next TopicAnagram in C

You may also like