Sequence Points in C | Set 1
Last Updated :
13 Oct, 2021
In this post, we will try to cover many ambiguous questions like following.
Guess the output of following programs.
C
// PROGRAM 1
#include <stdio.h>
int f1() { printf ("Geeks"); return 1;}
int f2() { printf ("forGeeks"); return 1;}
int main()
{
int p = f1() + f2();
return 0;
}
// PROGRAM 2
#include <stdio.h>
int x = 20;
int f1() { x = x+10; return x;}
int f2() { x = x-5; return x;}
int main()
{
int p = f1() + f2();
printf ("p = %d", p);
return 0;
}
// PROGRAM 3
#include <stdio.h>
int main()
{
int i = 8;
int p = i++*i++;
printf("%d\n", p);
}
The output of all of the above programs is
undefined or
unspecified. The output may be different with different compilers and different machines. It is like asking the value of undefined automatic variable.
The reason for undefined behavior in PROGRAM 1 is, the operator '+' doesn't have a standard defined order of evaluation for its operands. Either f1() or f2() may be executed first. So output may be either "GeeksforGeeks" or "forGeeksGeeks".
Similar to operator '+', most of the other similar operators like '-', '/', '*', Bitwise AND &, Bitwise OR |, .. etc don't have a standard defined order for evaluation for its operands.
Evaluation of an expression may also produce side effects. For example, in the above program 2, the final values of p are ambiguous. Depending on the order of expression evaluation, if f1() executes first, the value of p will be 55, otherwise 40.
The output of program 3 is also undefined. It may be 64, 72, or maybe something else. The subexpression i++ causes a side effect, it modifies i's value, which leads to undefined behavior since i is also referenced elsewhere in the same expression.
Unlike above cases,
at certain specified points in the execution sequence called sequence points, all side effects of previous evaluations are guaranteed to be complete. 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. Following are the sequence points listed in the C standard:
— The end of the first operand of the following operators:
a) logical AND &&
b) logical OR ||
c) conditional ?
d) comma ,
For example, the output of following programs is guaranteed to be "GeeksforGeeks" on all compilers/machines.
C
// Following 3 lines are common in all of the below programs
#include <stdio.h>
int f1() { printf ("Geeks"); return 1;}
int f2() { printf ("forGeeks"); return 1;}
// PROGRAM 4
int main()
{
// Since && defines a sequence point after first operand, it is
// guaranteed that f1() is completed first.
int p = f1() && f2();
return 0;
}
// PROGRAM 5
int main()
{
// Since comma operator defines a sequence point after first operand, it is
// guaranteed that f1() is completed first.
int p = (f1(), f2());
return 0;
}
// PROGRAM 6
int main()
{
// Since ? operator defines a sequence point after first operand, it is
// guaranteed that f1() is completed first.
int p = f1()? f2(): 3;
return 0;
}
— The end of a full expression. This category includes following expression statements
a) Any full statement ended with semicolon like "a = b;"
b) return statements
c) The controlling expressions of if, switch, while, or do-while statements.
d) All three expressions in a for statement.
The above list of sequence points is partial. We will be covering all remaining sequence points in the next post on Sequence Point.
References:
http://en.wikipedia.org/wiki/Sequence_point
http://c-faq.com/expr/seqpoints.html
http://msdn.microsoft.com/en-us/library/d45c7a5d(v=vs.110).aspx
http://www.open-std.org/jtc1/sc22/wg14/www/docs/n925.htm
Similar Reads
Longest Increasing Subsequence in C In this article, we will learn how to find the Longest Increasing Subsequence (LIS) of a given sequence using C programming language. LIS is the longest subsequence of a sequence such that all elements of the subsequence are sorted in increasing order.Example:Input:Sequence: [10, 22, 9, 33, 21, 50,
9 min read
strnset() function in C The strnset() function is a builtin function in C and it sets the first n characters of a string to a given character. If n is greater than the length of string, the length of string is used in place of n. Syntax: char *strnset(const char *str, char ch, int n); Parameters: str: This is the original
2 min read
Counting Set bit in C In C programming, counting set bits is the process of determining the number of bits that are set to 1 in a binary number. This operation is useful in various applications including network programming, error detection, and cryptography.In this article, we will learn how to count the number of set b
4 min read
C Language | Set 10 Following questions have been asked in GATE CS 2014 exam.1) Consider the following program in C language: C #include <stdio.h> main() { int i; int *pi = &i; scanf("%d", pi); printf("%d\n", i+5); } Which one of the following statements is TRUE? (A) Compilation fails. (B)
3 min read
Output of C Programs | Set 1 Predict the output of below programs. Question 1c#include<stdio.h> int main() { int n; for(n = 7; n!=0; n--) printf("n = %d", n--); getchar(); return 0; }Output: Above program goes in infinite loop because n is never zero when loop condition (n != 0) is checked.Question 2c #include<stdio.h
2 min read