Consider below C++ program and predict its output.
printf("%d %d %d", i, ++i, i++); |
The above invokes undefined behaviour by referencing both ‘i’ and ‘i++’ in the argument list. It is not defined in which order the arguments are evaluated. Different compilers may choose different orders. A single compiler can also choose different orders at different times.
For example below three printf statements may also cause undefined behavior.
// All three printf() statements // in this cause undefined behavior #include <stdio.h> int main() { volatile int a = 10; printf("\n %d %d", a, a++); a = 10; printf("\n %d %d", a++, a); a = 10; printf("\n %d %d %d ", a, a++, ++a); return 0; } |
Therefore, it is not recommended Not to do two or more than two pre or post increment operators in the same statement.
This means that there’s absolutely no temporal ordering in this process. The arguments can be evaluated in any order, and the process of their evaluation can be intertwined in any way.
This article is contributed by Spoorthi Aman. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
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.
Recommended Posts:
- Nested printf (printf inside printf) in C
- How to print % using printf()?
- Return values of printf() and scanf() in C/C++
- What is use of %n in printf() ?
- How to change the output of printf() in main() ?
- Passing NULL to printf in C
- Cin-Cout vs Scanf-Printf
- What is the difference between printf, sprintf and fprintf?
- Use of & in scanf() but not in printf()
- puts() vs printf() for printing a string
- Operators in C | Set 1 (Arithmetic Operators)
- Operators in C | Set 2 (Relational and Logical Operators)
- Measure execution time of a function in C++
- Measure execution time with high precision in C/C++
- Order of execution in initializer list in C++
- What are the operators that can be and cannot be overloaded in C++?
- Increment (Decrement) operators require L-value Expression
- Order of operands for logical operators
- Advanced C++ | Conversion Operators
- const_cast in C++ | Type Casting operators
Improved By : ArunSivaramakrishnanS

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
