Introduction
C is a structured, middle-level programming language that requires the use of a compiler to run programs written in it. The sequence point is defined as any point in the execution of a computer program at which all of the side effects of the previous evaluation of the program's code are completed or effectively completed. However, it also ensures that none of the later evaluations' modifications or side effects are carried out. In other words, a sequence point in imperative programming is any point in the execution of a computer program at which all of the side effects of the previous evaluation are assumed to have been completed.
Before we get into detail on Sequence points, it's important to grasp what are "side effects" we're talking about.
Also See, Sum of Digits in C, C Static Function
Side effects
The order in which expressions are evaluated is not specified in the C program. Since the compiler determines the sequence of evaluation based on context, some unexpected results may arise when using specific operators. These are termed as side effects.
Let's look at some examples to assist us understand the concept of side effects and practice it by yourself on C online compiler.
Example 1
Consider the following code Snippet:
#include <stdio.h>
int func1()
{
printf ("Hello \n");
return 1;
}
int func2()
{
printf ("Happy Coding!!!");
return 1;
}
int main()
{
int a = func1() + func2();
return 0;
}
Explanation :
The result of the preceding program is undefined or unpredictable because the output of the preceding program will vary depending on the machine or compiler because it is similar to asking for the value of an undefined automatic variable. The fundamental cause for the following program's undefined behaviour is that the "+" operator has no established standard sequence of evaluation for its operand. As a result, we are unaware of the execution of which function takes place first. Other operators that are similar to the "+" operator include '-,' '/,' '*,' Bitwise AND &, Bitwise OR |, and so on.
Let's look at another example for better understanding of side effects.
Example 2
Consider the following code Snippet:
#include <stdio.h>
int a = 28;
int func1()
{
a=a+12;
return a;
}
int func2()
{
a=a-14;
return a;
}
int main()
{
int x = func1() + func2();
printf("The value is %d" , x);
return 0;
}
Explanation:
The output of the above program is undefined as even evaluating the expression can have side effects. For example, the final value of x in the above program is confusing since it is entirely dependent on the sequence in which expressions are evaluated.
Let's look at another example of side effects on the unary operators.
Example 3
Consider the following Code Snippet:
int main()
{
int a = 3;
int x = a++*a++;
printf("%d\n", x);
}
Explanation:
The subexpression a++ has a side effect in that it changes the value of a, which creates undefined behaviour because a is also referenced elsewhere in the expression. Hence, the output of the code is also undefined.
So far we have discussed the details of the side effects . Now , let's understand the details of sequence points which help the compiler from the side effects.
Read about Bitwise Operators in C here.
Know more about Unary operator overloading in c++ in detail here.