Consider program C below:
#include <stdio.h> int f() { printf("f"); return 1; } int g() { printf("g"); return 2; } int main() { return f() + g(); }
According to the C standard, this program does not have one deterministic behavior due to the sum in the main function, which consists of two subexpressions and the following excerpt from the C99 standard:
§6.5 (...) the procedure for evaluating subexpressions and the order in which side effects occur are not specified.
Therefore, printing fg and gf are valid outputs for this program. In practice, this compiler will choose one fixed evaluation order (for example, from left to right for gcc in this case), but if I want to reliably compare the result between different compilers, I need to make sure that my program has one specific behavior.
My question is: what is the easiest way to do this? Is there a way to avoid including temporary variables (e.g. int tmp = f(); return tmp + g(); )?
source share