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


Groups > comp.programming > #1849

Re: Pre And Post Increment Operator Output

From "BartC" <bc@freeuk.com>
Newsgroups comp.programming
Subject Re: Pre And Post Increment Operator Output
Date 2012-06-22 16:02 +0100
Organization A noiseless patient Spider
Message-ID <js21jb$963$1@dont-email.me> (permalink)
References <8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com><js02hp$jsl$1@dont-email.me> <js03uf$sb3$1@dont-email.me><js092m$pov$1@dont-email.me> <lnipek8bri.fsf@nuthaus.mib.org><js1gf5$l7v$1@dont-email.me> <0.06d3eacd14619c86d384.20120622143458BST.87ehp7o4fx.fsf@bsb.me.uk>

Show all headers | View raw


[Moved from comp.lang.c]

"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.06d3eacd14619c86d384.20120622143458BST.87ehp7o4fx.fsf@bsb.me.uk...
> "BartC" <bc@freeuk.com> writes:

>>>>>> "Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> wrote in message
>>
>>>>>>>   int i = 0;
>>>>>>>   i = ++i + ++i + ++i;
>>>>>>>   printf("%d",i);
>>>>
>>>>>>> Output should be 6
>>>>>>>
>>>>>>> But In GCC it's 7
>>>>
BC>>>> I agree it ought to be 6.

>> OK, so we fix it so that the result is ....? Any guesses?
>
> Argh!  Another chasm opens up!  There can be no guess.  You fix it by
> choosing what you want it to mean.  You choose it to mean 6, so you "fix
> it" (in all senses of the word) by re-writing it as:
...

> It doesn't just "happen" to coincide -- you forced it to.  You have a
> view of what the code "really" means, but that's just one rather
> arbitrary point of view.  To take another, ++i, when i is zero, sets i
> to 1 and has the value one.  The statement
>
>  i = ++i + ++i + ++i;
>
> could, in this context, be taken to mean
>
>  i = (i = 1) + (i = 1) + (i = 1);
>
> which is still undefined of course but it looks quite different and your
> expectation would probably be different.

This is all true. But I still think that if you ask for opinions as to the
result of i=(++i)+(++i)+(++i), when i starts at 0, *outside the context of
the C language*, then 6 is a strong contender.

I don't immediately have many other languages to hand that have a "++"
operator, but trying this sort of thing with Python, Lua and Ruby:

i=0

def incri():
        global i
        i=i+1
        return i

i=(incri()+incri()+incri())
print (i)

they all give 6 as the result. Trying it on 4 of my (own) compilers, (on one
of which I had to rush through the implementation of ++!) they all give 6
too.

Even in C, when the expression was simplified to 3-address code, the result
was 6. And in the compiler in my head, the answer was also 6!

So although it was understood why C might not give 6 for the original
expression, you must understand that expecting a value of 6, for someone new
to C, was perfectly reasonable. Hence I agreed with the OP.

(Sorry to drag the discussion here, but it was becoming uncomfortable in
comp.lang.c)

-- 
Bartc

 

Back to comp.programming | Previous | NextNext in thread | Find similar


Thread

Re: Pre And Post Increment Operator Output "BartC" <bc@freeuk.com> - 2012-06-22 16:02 +0100
  Re: Pre And Post Increment Operator Output Patricia Shanahan <pats@acm.org> - 2012-06-22 08:30 -0700
    Re: Pre And Post Increment Operator Output "BartC" <bc@freeuk.com> - 2012-06-22 17:49 +0100
      Re: Pre And Post Increment Operator Output Patricia Shanahan <pats@acm.org> - 2012-06-22 10:36 -0700
        Re: Pre And Post Increment Operator Output "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> - 2012-06-23 09:57 +0100
      Re: Pre And Post Increment Operator Output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-22 20:53 +0100
        Re: Pre And Post Increment Operator Output "BartC" <bc@freeuk.com> - 2012-06-22 21:35 +0100
          Re: Pre And Post Increment Operator Output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-22 23:19 +0100
            Re: Pre And Post Increment Operator Output "BartC" <bc@freeuk.com> - 2012-06-23 01:15 +0100
  Re: Pre And Post Increment Operator Output Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-06-22 17:44 +0100

csiph-web