It seemed to me that I understand how sequence points work in C ++, but this GeeksQuiz question puzzled me:
int f(int &x, int c) { c = c - 1; if (c == 0) return 1; x = x + 1; return f(x, c) * x; } int main() { int p = 5; cout << f(p, p) << endl; return 0; }
The โcorrectโ answer to this question says that it prints 6561. Indeed, in VS2013 it does. But is it not UB, because there is no guarantee that is first evaluated: f(x, c) or x . We get 6561 if f(x, c) is first evaluated: it all turns into five recursive calls: the first four (c = 5, 4, 3, 2 ) continue, the last (c = 1) completes and returns 1, which is equal to 9 ** 4 at the end.
However, if x was first evaluated, then we would get 6 * 7 * 8 * 9 * 1 . The funny thing is that in VS2013 even replacing f(x, c) * x with x * f(x, c) will not change the result. Not that it meant anything.
According to the standard, is it UB or not? If not, why?
source share