Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #10103

Re: Unexpected output

From Eric Sosman <esosman@ieee-dot-org.invalid>
Newsgroups comp.lang.c
Subject Re: Unexpected output
Date 2011-09-10 16:42 -0400
Organization A noiseless patient Spider
Message-ID <j4gi57$taa$1@dont-email.me> (permalink)
References <8787e0b5-b4c3-463f-bd9a-6ffc30034950@x29g2000prh.googlegroups.com> <chine.bleu-E38952.12592910092011@news.eternal-september.org>

Show all headers | View raw


On 9/10/2011 3:59 PM, China Blue Corn Chips wrote:
> In article<8787e0b5-b4c3-463f-bd9a-6ffc30034950@x29g2000prh.googlegroups.com>,
>   Mazen<neurohacks@gmail.com>  wrote:
>[...]
>>    printf("%d %d\n", i=+j, i=-j);
>>  [...]
>> The output is
>> 2 2
>>
>> Shouldn't the second expression be -2 rather than 2? I'm confused.
>> Your insights pls. Thanks!
>
> The order of evaluation of function arguments is left to the implementation.

     It's more serious than that: The behavior is undefined.  The
line in question tries to assign to `i' twice without an intervening
sequence point, and the C language makes no promises whatsoever about
what might happen in consequence.

     C has two distinct uses for the comma: as an operator, and as
a separator.  The comma operator involves a defined order and a
sequence point: `f = x,y' means "first evaluate x and ignore the
value, sequence point, then evaluate y and yield its value, that
value is assigned to f."  The comma separator just separates the
elements of a list, without implying any kind of order and without 
imposing sequence points.  (Unfortunately, the two uses look
distressingly similar.)

     In the offending line above, both commas are separators rather
than operators, so no sequence points divide the argument evaluations
from each other.  In particular, the two assignments to `i' are not
so divided, hence the undefined behavior.

-- 
Eric Sosman
esosman@ieee-dot-org.invalid

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 12:36 -0700
  Re: Unexpected output China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-10 12:59 -0700
    Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-10 16:42 -0400
      Re: Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 17:51 -0700
        Re: Unexpected output Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-10 20:38 -0600
        Re: Unexpected output pete <pfiland@mindspring.com> - 2011-09-10 22:41 -0400
        Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-11 09:35 -0400
          Re: Unexpected output gazelle@shell.xmission.com (Kenny McCormack) - 2011-09-11 13:39 +0000
        Re: Unexpected output "BartC" <bc@freeuk.com> - 2011-09-11 15:26 +0100
      Re: Unexpected output Paul N <gw7rib@aol.com> - 2011-09-11 12:08 -0700
        Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-11 17:43 -0400
    Re: Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 13:24 -0700
      Re: Unexpected output China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-10 15:02 -0700
    Re: Unexpected output Kenneth Brody <kenbrody@spamcop.net> - 2011-09-12 10:01 -0400
  Re: Unexpected output arni <arnmin@gmail.com> - 2011-09-10 13:31 -0700
    Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-10 16:49 -0400
      Re: Unexpected output arni <arnmin@gmail.com> - 2011-09-10 14:13 -0700
        Re: Unexpected output Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-10 17:26 -0400
          Re: Unexpected output Mazen <neurohacks@gmail.com> - 2011-09-10 17:59 -0700
            Re: Unexpected output Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-10 20:52 -0600
              Re: Unexpected output China Blue Corn Chips <chine.bleu@yahoo.com> - 2011-09-10 19:55 -0700
              Re: Unexpected output Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-11 06:03 -0700
                Re: Unexpected output James Kuyper <jameskuyper@verizon.net> - 2011-09-11 14:04 -0400
            Re: Unexpected output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-11 11:22 +0100
            Re: Unexpected output Keith Thompson <kst-u@mib.org> - 2011-09-11 11:01 -0700
              Re: Unexpected output Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-09-13 14:09 +0300
        Re: Unexpected output Seebs <usenet-nospam@seebs.net> - 2011-09-13 06:44 +0000
    Re: Unexpected output "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-09-10 17:16 -0400
  Re: Unexpected output "BartC" <bc@freeuk.com> - 2011-09-10 23:20 +0100
  Re: Unexpected output August Karlstrom <fusionfile@gmail.com> - 2011-09-11 22:55 +0200
    Re: Unexpected output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-11 22:51 +0100
      Re: Unexpected output pete <pfiland@mindspring.com> - 2011-09-11 22:25 -0400
      Re: Unexpected output August Karlstrom <fusionfile@gmail.com> - 2011-09-12 15:34 +0200
        Re: Unexpected output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-09-12 19:55 +0100
          Re: Unexpected output August Karlstrom <fusionfile@gmail.com> - 2011-09-13 19:22 +0200

csiph-web