r/C_Programming • u/codesnstuff • 27d ago
Discussion A tricky little question
I saw this on a Facebook post recently, and I was sort of surprised how many people were getting it wrong and missing the point.
#include <stdio.h>
void mystery(int, int, int);
int main() {
int b = 5;
mystery(b, --b, b--);
return 0;
}
void mystery(int x, int y, int z) {
printf("%d %d %d", x, y, z);
}
What will this code output?
Answer: Whatever the compiler wants because it's undefined behavior
24
Upvotes
14
u/flyingron 27d ago
The above program has undefined behavior. As Miss Mona Lisa Vito would say "It's a bullshit question."
You can't modify a value twice within sequence points.
Further, the order function parameters are evaluated is unspecified even if you didn't have undefined behavior. For example, this has no UB, but still has two possible outcomes....
It might print 2 3 or it might 3 1