Can anyone tell me why the output of the following code is as given?

J

jaffarkazi

The code is:

int x = 15;
printf("%d %d\n", (x != 15), (x = 1));
x = 15;
printf("%d %d\n", (x = 1), (x != 15));

The output is:
1 1
1 1

If the , operator goes from right to left, then one of the cases
should give the o/p of (x != 15) as 0.

Regards,
--Jaffar
 
V

vippstar

The code is:

int x = 15;
printf("%d %d\n", (x != 15), (x = 1));
x = 15;
printf("%d %d\n", (x = 1), (x != 15));

The output is:
1 1
1 1

If the , operator goes from right to left, then one of the cases
should give the o/p of (x != 15) as 0.

(a, b, c); is guaranteed to evaluate first 'a', then 'b', then 'c'.

f(a, b, c); is not the same; These can be evaluated in any order.
 
W

Willem

jaffarkazi wrote:
) The code is:
)
) int x = 15;
) printf("%d %d\n", (x != 15), (x = 1));
) x = 15;
) printf("%d %d\n", (x = 1), (x != 15));
)
) The output is:
) 1 1
) 1 1
)
) If the , operator goes from right to left, then one of the cases
) should give the o/p of (x != 15) as 0.

There is no , operator in that piece of code.
Just a , separator in the function calls.
That's something completely different.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
N

Nick Keighley

(a, b, c); is guaranteed to evaluate first 'a', then 'b', then 'c'.

f(a, b, c); is not the same; These can be evaluated in any order.

ie. the example you gave is not an example of the comma operator
 
R

Richard Bos

jaffarkazi said:
printf("%d %d\n", (x != 15), (x = 1));

This causes undefined behaviour. You are assigning to x, and evaluating
x _not_ for the purposes of the assignment, without an intervening
sequence point. Anything may happen; weird numbers is the most likely
outcome, as you observed, but a crash, though unlikely, is possible.
printf("%d %d\n", (x = 1), (x != 15));
If the , operator goes from right to left,

It does, but there are no , operators in the above statements. The ,
between function call arguments is not the same as the , operator.

Richard
 
M

Martin Ambuhl

jaffarkazi said:
The code is:

int x = 15;
printf("%d %d\n", (x != 15), (x = 1));
x = 15;
printf("%d %d\n", (x = 1), (x != 15));

The output is:
1 1
1 1

If the , operator goes from right to left, then one of the cases
should give the o/p of (x != 15) as 0.

There is no comma operator in your snippet. Since there is no comma
operator, whatever conclusions you draw are irrelevant. And if there
were a comma operator, the evaluation order is left-to-right, anyway.
The ','s in you snippet separate arguments and are not a comma
operators, and the order of evaluation of arguments is an implementation
detail, not defined by the standard. Many such errors are the result of
(1) trying to be too clever by packing too much into single statements
and (2) not be clever enough to know what that single statement does.
 
M

Martin Ambuhl

Richard said:

Since when?
In the (meta-linguistic) statement
<left-expression>, <right-expression>;
The <left-expression> is evaluatied first. The order of evaluation is
left-to-right.
 
S

santosh

Nick said:
ie. the example you gave is not an example of the comma operator

Why not? His first example, i.e., (a, b, c); is a perfectly valid
illustration of the comma operator, AFAICS. Here is an example:

#include <stdio.h>

int main(void) {
int a, b, c;
(a = 0, b = a+1, c = b+1);
printf("a = %d\tb = %d\tc = %d\n", a, b, c);
return 0;
}

Output:
a = 0 b = 1 c = 2
 
K

Keith Thompson

santosh said:
Why not? His first example, i.e., (a, b, c); is a perfectly valid
illustration of the comma operator, AFAICS.
[...]

I think what Nick meant is that jaffarkazi didn't give an exmaple of
the comma operator. vippstar did give such an exmaple, to demonstrate
what jaffarkazi was actually doing. Nick, I believe, was emphasizing
and clarifying vippstar's point.
 
S

santosh

Keith said:
santosh said:
Why not? His first example, i.e., (a, b, c); is a perfectly valid
illustration of the comma operator, AFAICS.
[...]

I think what Nick meant is that jaffarkazi didn't give an exmaple of
the comma operator. vippstar did give such an exmaple, to demonstrate
what jaffarkazi was actually doing. Nick, I believe, was emphasizing
and clarifying vippstar's point.

Oops. Didn't see it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top