In this post, we will try to cover many ambiguous questions like following.
Guess the output of following programs.
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 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 is 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 may be 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.
— 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.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print numbers in sequence using thread synchronization
- Count number of unique Triangles using STL | Set 1 (Using set)
- C++ Internals | Default Constructors | Set 1
- Exception handling and object destruction | Set 1
- Understanding "volatile" qualifier in C | Set 2 (Examples)
- How to count set bits in a floating point number in C?
- Namespace in C++ | Set 1 (Introduction)
- Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
- Sorting 2D Vector in C++ | Set 1 (By row and column)
- C++ String Class and its Applications | Set 2
- Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second)
- Sorting Vector of Pairs in C++ | Set 2 (Sort in descending order by first and second)
- Sorting 2D Vector in C++ | Set 3 (By number of columns)
- Set a variable without using Arithmetic, Relational or Conditional Operator
- Forward List in C++ | Set 1 (Introduction and Important Functions)
- Forward List in C++ | Set 2 (Manipulating Functions)
- Ratio Manipulations in C++ | Set 1 (Arithmetic)
- Ratio Manipulations in C++ | Set 2 (Comparison)
- namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)
- numeric header in C++ STL | Set 2 (adjacent_difference(), inner_product() and iota())