I just compiled the program below in Cygwin and on codepad
int main()
{
int k=1;
printf("%d, %d, %d\n", k, k++, k++);
}
In both cases the output was
3, 2, 1
Shouldn't it be the other way around?
Any thoughts would be welcome.
![]()
This is a discussion on Odd printf behavior? within the C Programming forums, part of the General Programming Boards category; I just compiled the program below in Cygwin and on codepad int main() { int k=1; printf("%d, %d, %d\n", k, ...
I just compiled the program below in Cygwin and on codepad
int main()
{
int k=1;
printf("%d, %d, %d\n", k, k++, k++);
}
In both cases the output was
3, 2, 1
Shouldn't it be the other way around?
Any thoughts would be welcome.
![]()
You're engaging in undefined behavior due to sequence points. If you're being taught this, your instructor is an idiot.
It is undefined what order the parameters of a function are evaluated in. In practice, many compilers will do them in the reverse order because that's how they get pushed on the stack, but don't depend on that.
Another dupe thread -> Odd printf behavior?
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
I support http://www.ukip.org/ as the first necessary step to a free Europe.