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

    jaffarkazi Guest

    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
    #1
    1. Advertising

  2. jaffarkazi

    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
    #2
    1. Advertising

  3. jaffarkazi

    Willem Guest

    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
    #3
  4. 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
    #4
  5. jaffarkazi

    Richard Bos Guest

    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
    #5
  6. 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
    #6
  7. 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
    #7
  8. jaffarkazi

    santosh Guest

    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
    #8
  9. 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
    #9
  10. jaffarkazi

    santosh Guest

    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
    #10
  11. jaffarkazi

    Richard Bos Guest

    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
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,036
    Smokey Grindel
    Dec 2, 2006
  2. yogesh
    Replies:
    1
    Views:
    372
    Victor Bazarov
    Mar 14, 2007
  3. manish sahu
    Replies:
    3
    Views:
    1,012
  4. SaikiranPuppala
    Replies:
    0
    Views:
    466
    SaikiranPuppala
    Jul 10, 2008
  5. Adam Penny
    Replies:
    4
    Views:
    109
    Brian Candler
    Oct 21, 2008
Loading...

Share This Page