problem with c code

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
john_locke
New poster
Posts: 13
Joined: Sat Oct 07, 2006 6:42 pm
Contact:

problem with c code

Post by john_locke »

hi i have an problem with my c code
#include<iostream>
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
little joey
Guru
Posts: 1080
Joined: Thu Dec 19, 2002 7:37 pm

Post 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.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post 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.
#include<iostream>
using namespace std;
This can't be C. It's C++.
john_locke
New poster
Posts: 13
Joined: Sat Oct 07, 2006 6:42 pm
Contact:

Post by john_locke »

thanx to both of you i got the point
john_locke
New poster
Posts: 13
Joined: Sat Oct 07, 2006 6:42 pm
Contact:

Post by john_locke »

dear MF

the link provided by you is not working can u give some another link.
mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:

Post by mf »

can u give some another link.
Chapter 2, section 2.12, in this book.
Post Reply

Return to “C”