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

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

jaffarkazi
Guest
Posts: n/a

 07-03-2008
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

vippstar@gmail.com
Guest
Posts: n/a

 07-03-2008
On Jul 3, 6:28 pm, jaffarkazi <(E-Mail Removed)> 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.

(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.

Willem
Guest
Posts: n/a

 07-03-2008
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

Nick Keighley
Guest
Posts: n/a

 07-03-2008
On 3 Jul, 16:33, (E-Mail Removed) wrote:
> On Jul 3, 6:28 pm, jaffarkazi <(E-Mail Removed)> 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.

>
> (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

--
Nick Keighley

Richard Bos
Guest
Posts: n/a

 07-03-2008
jaffarkazi <(E-Mail Removed)> wrote:

> 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

Martin Ambuhl
Guest
Posts: n/a

 07-03-2008
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 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.

Martin Ambuhl
Guest
Posts: n/a

 07-03-2008
Richard Bos wrote:
> jaffarkazi <(E-Mail Removed)> wrote:

>> If the , operator goes from right to left,

>
> It does,

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.

santosh
Guest
Posts: n/a

 07-03-2008
Nick Keighley wrote:

> On 3 Jul, 16:33, (E-Mail Removed) wrote:
>> On Jul 3, 6:28 pm, jaffarkazi <(E-Mail Removed)> 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.

>>
>> (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

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

Keith Thompson
Guest
Posts: n/a

 07-03-2008
santosh <(E-Mail Removed)> writes:
> Nick Keighley wrote:
>> On 3 Jul, 16:33, (E-Mail Removed) wrote:
>>> On Jul 3, 6:28 pm, jaffarkazi <(E-Mail Removed)> 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.
>>>
>>> (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

>
> 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.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

santosh
Guest
Posts: n/a

 07-03-2008
Keith Thompson wrote:

> santosh <(E-Mail Removed)> writes:
>> Nick Keighley wrote:
>>> On 3 Jul, 16:33, (E-Mail Removed) wrote:
>>>> On Jul 3, 6:28 pm, jaffarkazi <(E-Mail Removed)> 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.
>>>>
>>>> (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

>>
>> 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.