Page 1 of 1

problem with c code

Posted: Tue Feb 13, 2007 10:32 pm
by john_locke
hi i have an problem with my c code
using namespace std;
int main()
int k=35;
printf("%d %d %d %d ",k==35,k=50,k,k>40);
return 0;

why the output of these problem is

0 50 35 0

Posted: Tue Feb 13, 2007 11:00 pm
by little joey
The order in which arguments of a function call are evaluated is undetermined (the comma as separator in a function call is not the same as the comma as sequencing operator), so in your case the evaluation order might be backwards (which would be consistent with the output, but don't rely on it).
PS. Your code is C++, not C.

Posted: Tue Feb 13, 2007 11:14 pm
by mf
C standard doesn't specify order in which function's arguments are evaluated, so it can depend on your compiler, compile flags, phase of moon, etc...
More on order of evaluation in C here

In your case, it seems that the compiler evaluated arguments from right to left.
using namespace std;
This can't be C. It's C++.

Posted: Mon Mar 19, 2007 7:57 am
by john_locke
thanx to both of you i got the point

Posted: Tue Mar 20, 2007 7:44 am
by john_locke
dear MF

the link provided by you is not working can u give some another link.

Posted: Tue Mar 20, 2007 9:27 am
by mf
can u give some another link.
Chapter 2, section 2.12, in this book.