#include<stdio.h>
int main(void)
{
int i=5;
printf("%d", i++ + ++i);
return 0;
}
I know this is undefined behaviour (or implementation defined) and should not be used. But the compiler does give an output without giving any warning. So is there any to predict the program’s output?
Is there any way to predict the Undefined Behaviour?
NO
Undefined Behavior means that compiler does not need to adhere to any specific behavior, every compiler may or may not show the same behavior. You cannot rely on approximating/predicting outputs of Undefined behavior from the compiler and write a code on the basis of that.
Strictly, avoid writing any code which invokes Undefined Behavior.
Reference:
Undefined behaviour (UB) is defined by the ISO/ANSI C Standard as:
Is there any way to predict Implementation defined behaviour?
Yes, If Portability(assurance that your solution works across different compilers in same way) is not your concern.
No, If you are looking for portability.
If you are working on a specific compiler then,and your solution/project needs to only work for that particular compiler and environment then you can take the liberty to use the implementation specific behavior displayed by that compiler on that environment.
This should be an Interesting read:
A Guide to Undefined Behavior in C and C++.