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

Discussion in 'C Programming' started by jaffarkazi, Jul 3, 2008.

1. jaffarkaziGuest

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

jaffarkazi, Jul 3, 2008

2. Guest

Re: Can anyone tell me why the output of the following code is asgiven?

On Jul 3, 6:28 pm, 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.

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

, Jul 3, 2008

3. WillemGuest

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

Willem, Jul 3, 2008
4. Nick KeighleyGuest

Re: Can anyone tell me why the output of the following code is asgiven?

On 3 Jul, 16:33, wrote:
> On Jul 3, 6:28 pm, 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.

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

Nick Keighley, Jul 3, 2008
5. Richard BosGuest

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

Richard Bos, Jul 3, 2008
6. Martin AmbuhlGuest

Re: Can anyone tell me why the output of the following code is asgiven?

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, Jul 3, 2008
7. Martin AmbuhlGuest

Re: Can anyone tell me why the output of the following code is asgiven?

Richard Bos wrote:
> jaffarkazi <> 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.

Martin Ambuhl, Jul 3, 2008
8. santoshGuest

Nick Keighley wrote:

> On 3 Jul, 16:33, wrote:
>> On Jul 3, 6:28 pm, 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.

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

santosh, Jul 3, 2008
9. Keith ThompsonGuest

santosh <> writes:
> Nick Keighley wrote:
>> On 3 Jul, 16:33, wrote:
>>> On Jul 3, 6:28 pm, 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.
>>>
>>> (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.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, Jul 3, 2008
10. santoshGuest

Keith Thompson wrote:

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

santosh, Jul 3, 2008
11. Richard BosGuest

Martin Ambuhl <> wrote:

> Richard Bos wrote:
> > jaffarkazi <> wrote:

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

> >
> > It does,

>
> Since when?

Erm... since someone reversed the polarity of the neutron flow, so that
left is now right and vice versa...

Richard

Richard Bos, Jul 4, 2008