Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Side effects
2.1.
Example 1
2.2.
Example 2
2.3.
Example 3
3.
Sequence points
3.1.
Logical AND (&&)
3.2.
Logical OR (||)
3.3.
Conditional(?)
3.4.
Comma(,)
4.
Frequently Asked Questions
4.1.
What are sequence points in C?
4.2.
What are side effects in c?
4.3.
What are some common sequence points?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Sequence Points in C

Author Nagendra
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

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.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Sequence points

Any action that changes the storage of an operand has a side effect. The programmer can intentionally cause side effects to achieve the desired result; in fact, the assignment operator relies on the side effect of changed storage to function. By the following sequence point in the program, C assures that all side effects of a particular expression will be fulfilled. Sequence points are checkpoints in a program where the compiler verifies that an expression's operations are completed.
The most basic sequence points in the C programming language are :

  • Logical AND (&&)
  • Logical OR(||)
  • Conditional(?)
  • Comma(,)

Logical AND (&&)

The left operand of the logical AND (&&) operator will be fully performed first, including all side effects, before proceeding. The execution process will halt if the left operand evaluates to false, and the other operand will not be executed at all.
Lets understand the working of Logical AND (&&) through a small program.
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;  
}

Output:

Hello
Happy Coding!!!

Explanation:

Since Logical And(&&) defines a sequence point after the first operand, func1() will always be performed first.
 

Also see,  Short int in C Programming

Logical OR (||)

When using logical OR (||), the left operand must be fully performed first, together with all of its side effects, before proceeding. If the left operand evaluates to false (or nonzero), however, the other operand is not executed.
Lets understand the working of Logical OR (||) through a small program.
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;  
}

Output:

Hello

Explanation:

Since Logical OR (||) defines a sequence point after the first operand, func1() will always be performed first. Since the func1() returns 1 (true) ,so func2() is not executed.

You can implement the code on online C editor.

Conditional(?)

When using the conditional operator, the first operand will be evaluated first, and all of its side effects will be completed before proceeding.
Lets understand the working of Conditional (?) through a small program.
Consider the following code :

#include <stdio.h>  
int func1()
{
    printf ("Hello");
    return 1;
    }  
int func2()
{
     printf ("Happy Coding");
     return 1;
     }  
int main()  
{  
  int p = func1() ? func2() : 3 ;    
  return 0;  
}

Output :

Hello
Happy Coding!!!

Explanation:

Since conditional (?) defines a sequence point after the first operand, func1() will always be performed first.

Comma(,)

When using the comma (,), the first operand will be evaluated first, and all of its side effects will be completed before proceeding.
Lets understand the working of Comma (,) through a small program.
Consider the following code :

#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;  
}

Output:

Hello
Happy Coding!!!

Explanation:

Since comma (,) defines a sequence point after the first operand, func1() will always be performed first.

Check out this problem - Shortest Common Supersequence,Tribonacci Series

Must Read what is storage class in c and Decision Making in C

Frequently Asked Questions

What are sequence points in C?

Sequence points are checkpoints in a program where the compiler verifies that an expression's operations are completed.

What are side effects in c?

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.

What are some common sequence points?

The most basic sequence points in the C programming language are Logical AND (&&), Logical OR(||), Conditional(?), and Comma(,).

Conclusion

In this article, we have extensively discussed the results of comparison operations in C and C++. The article explains the variation of the result of comparison operators in C and C++.
We hope that this blog has helped you enhance your knowledge regarding comparison operations in C and C++ and if you would like to learn more, check out our articles on C. You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc. To practice and improve yourself in the interview, you can check out Top 100 SQL problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews.
Do upvote our blog to help other ninjas grow. 

You can also try include stdio h by clicking here.

Happy Coding!!

Previous article
Results of comparison operations in C and C++
Next article
Comma Operator in C
Live masterclass