Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #1849 > unrolled thread
| Started by | "BartC" <bc@freeuk.com> |
|---|---|
| First post | 2012-06-22 16:02 +0100 |
| Last post | 2012-06-22 17:44 +0100 |
| Articles | 10 — 4 participants |
Back to article view | Back to comp.programming
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
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
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-06-22 16:02 +0100 |
| Subject | Re: Pre And Post Increment Operator Output |
| Message-ID | <js21jb$963$1@dont-email.me> |
[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
[toc] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2012-06-22 08:30 -0700 |
| Message-ID | <7MudnTZhnIs6EnnSnZ2dnUVZ_qCdnZ2d@earthlink.com> |
| In reply to | #1849 |
On 6/22/2012 8:02 AM, BartC wrote: ... > 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. In Java, i does have a specified resulting value. The effect will be the same as i=3*i+6. I don't understand what you mean by "outside the context of the C language". How can a code snippet have any meaning at all except in the context of a programming language? Patricia
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-06-22 17:49 +0100 |
| Message-ID | <js27pl$h7c$1@dont-email.me> |
| In reply to | #1850 |
"Patricia Shanahan" <pats@acm.org> wrote in message news:7MudnTZhnIs6EnnSnZ2dnUVZ_qCdnZ2d@earthlink.com... > On 6/22/2012 8:02 AM, BartC wrote: > ... >> 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. > > In Java, i does have a specified resulting value. The effect will be the > same as i=3*i+6. So it ends up with i=6, when i starts at zero. The case for '6' is improving.. > > I don't understand what you mean by "outside the context of the C > language". How can a code snippet have any meaning at all except in the > context of a programming language? In C specifically, such an expression has an undefined value (undefined behaviour actually). You're not allowed, apparently, to ponder what the actual result will be, nor what the expectation is, nor even what the intention of coder was. But given suitable definitions of "=" (assignment), "+", and "++" (increment in-place), which are fairly standard anyway, I think you ought to be allowed to suggest a likely value of i after: i=0 i=(++i) + (++i) + (++i) without a particular language in mind. I suggested in comp.lang.c that the result of the above ought to be 6, in line with the original poster's expectations, (before going on to explain why it might not be in C), for which I was castigated and even called evil. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Patricia Shanahan <pats@acm.org> |
|---|---|
| Date | 2012-06-22 10:36 -0700 |
| Message-ID | <kJ6dnXFGOIG6MHnSnZ2dnUVZ_gidnZ2d@earthlink.com> |
| In reply to | #1854 |
On 6/22/2012 9:49 AM, BartC wrote: > > > "Patricia Shanahan" <pats@acm.org> wrote in message > news:7MudnTZhnIs6EnnSnZ2dnUVZ_qCdnZ2d@earthlink.com... >> On 6/22/2012 8:02 AM, BartC wrote: >> ... >>> 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. >> >> In Java, i does have a specified resulting value. The effect will be the >> same as i=3*i+6. > > So it ends up with i=6, when i starts at zero. The case for '6' is > improving.. The case for 6 is absolutely solid *in Java*. The case for it being a syntactically correct expression with undefined result is equally solid in C. In any other language, whether it is even syntactically valid code, and if so what it means, depends on entirely rules that we don't know without knowing the language. Note that most experiments can ever say is "In language X, 6 is one of the possible outcomes". I'm willing to make a much stronger statement about Java not because of experiments, but because of what the Java Language Specification says about expression evaluation. Patricia
[toc] | [prev] | [next] | [standalone]
| From | "Chris Uppal" <chris.uppal@metagnostic.REMOVE-THIS.org> |
|---|---|
| Date | 2012-06-23 09:57 +0100 |
| Message-ID | <8pKdnfKKENoZGHjSnZ2dnUVZ8oydnZ2d@bt.com> |
| In reply to | #1855 |
Patricia Shanahan wrote:
> The case for 6 is absolutely solid *in Java*. The case for it being a
> syntactically correct expression with undefined result is equally solid
> in C. In any other language, whether it is even syntactically valid
> code, and if so what it means, depends on entirely rules that we don't
> know without knowing the language.
I'm not sure that holds water. In some language with roughly C/Java like
syntax, and which /didn't/ produce 6 for this example, then it would certainly
be reasonable to warn beginers that "it doesn't do what you might expect". The
warning has meaning, even positive value, but it couldn't have either if there
wasn't some sense to "what you might expect" independent of the language spec.
Come to that, might not even /be/ a spec, anyway.
-- chris
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-06-22 20:53 +0100 |
| Message-ID | <0.0b3aeb4b00dfdccbd115.20120622205336BST.87395nnmwv.fsf@bsb.me.uk> |
| In reply to | #1854 |
"BartC" <bc@freeuk.com> writes: > "Patricia Shanahan" <pats@acm.org> wrote in message > news:7MudnTZhnIs6EnnSnZ2dnUVZ_qCdnZ2d@earthlink.com... >> On 6/22/2012 8:02 AM, BartC wrote: >> ... >>> 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. >> >> In Java, i does have a specified resulting value. The effect will be the >> same as i=3*i+6. > > So it ends up with i=6, when i starts at zero. The case for '6' is > improving.. The case for 6 in Java is rock-solid. <snip> > I suggested in comp.lang.c that the result of the above ought to be 6, > in line with the original poster's expectations, (before going on to > explain why it might not be in C), for which I was castigated and even > called evil. Way to go BartC. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-06-22 21:35 +0100 |
| Message-ID | <js2l17$cd9$1@dont-email.me> |
| In reply to | #1856 |
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message news:0.0b3aeb4b00dfdccbd115.20120622205336BST.87395nnmwv.fsf@bsb.me.uk... > "BartC" <bc@freeuk.com> writes: >> "Patricia Shanahan" <pats@acm.org> wrote in message >>> On 6/22/2012 8:02 AM, BartC wrote: >>>> result of i=(++i)+(++i)+(++i), when i starts at 0 >>> In Java, i does have a specified resulting value. The effect will be the >>> same as i=3*i+6. >> >> So it ends up with i=6, when i starts at zero. The case for '6' is >> improving.. > > The case for 6 in Java is rock-solid. > > <snip> >> I suggested in comp.lang.c that the result of the above ought to be 6, >> in line with the original poster's expectations, (before going on to >> explain why it might not be in C), for which I was castigated and even >> called evil. > > Way to go BartC. Maybe the OP in the original thread had a Java background. Or maybe he just decided the common-sense result was 6, and wanted to know why C didn't agree. But I give up; you win. Clearly if someone sees such an expression, even before they go anywhere near an actual programming language, they have to assume that *expecting* a result such as 94613277, is just as plausible as expecting it to be as 6. (BTW how did Java, a C derivative, end up with well-defined semantics for this kind of expression? And why isn't the result 94613227, but... 6? Why choose 6? Because, after all, you guys keep telling me that i = (++i)+(++i)+(++i) is completely meaningless. Or is it conceivable that 6 does, after all, actually make more sense than 94613277..) Anyway, I'll leave you guys to it. -- Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-06-22 23:19 +0100 |
| Message-ID | <0.6a8ba340e934c61ead30.20120622231916BST.87wr2zm1ln.fsf@bsb.me.uk> |
| In reply to | #1857 |
"BartC" <bc@freeuk.com> writes: > "Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message > news:0.0b3aeb4b00dfdccbd115.20120622205336BST.87395nnmwv.fsf@bsb.me.uk... >> "BartC" <bc@freeuk.com> writes: >>> "Patricia Shanahan" <pats@acm.org> wrote in message >>>> On 6/22/2012 8:02 AM, BartC wrote: > >>>>> result of i=(++i)+(++i)+(++i), when i starts at 0 > >>>> In Java, i does have a specified resulting value. The effect will be the >>>> same as i=3*i+6. >>> >>> So it ends up with i=6, when i starts at zero. The case for '6' is >>> improving.. >> >> The case for 6 in Java is rock-solid. >> >> <snip> >>> I suggested in comp.lang.c that the result of the above ought to be 6, >>> in line with the original poster's expectations, (before going on to >>> explain why it might not be in C), for which I was castigated and even >>> called evil. >> >> Way to go BartC. > > Maybe the OP in the original thread had a Java background. Or maybe he just > decided the common-sense result was 6, and wanted to know why C didn't > agree. Maybe. So what? The OP got told by a few people (including you) that the result is undefined in C. I don't think anyone had any problem with the OP other than the fact that this question gets asked a lot -- there's something similar in the FAQ -- but people don't check either the archives of the FAQ much these days. > But I give up; you win. Clearly if someone sees such an expression, even > before they go anywhere near an actual programming language, they have to > assume that *expecting* a result such as 94613277, is just as plausible as > expecting it to be as 6. No they don't. They can ask in comp.lang.c for one thing. The OP did and they got an answer. It was all quite civil. But you know C's rules, so why did say the value *ought* to be 6. What was that about? Did you want to re-start the debate about the sequence point rules? Did you want to propose a new semantics for C expressions? If you did, you didn't present a case for one. It's hardly surprising that someone (not me) thought you might just be winding people up. > (BTW how did Java, a C derivative, end up with well-defined semantics for > this kind of expression? And why isn't the result 94613227, but... 6? Why > choose 6? Because, after all, you guys keep telling me that i = > (++i)+(++i)+(++i) is completely meaningless. Or is it conceivable that > 6 does, after all, actually make more sense than 94613277..) > > Anyway, I'll leave you guys to it. Yes, right after another gross miss-characterisation of what was said. As I said before, way to go. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2012-06-23 01:15 +0100 |
| Message-ID | <js31tp$obe$1@dont-email.me> |
| In reply to | #1858 |
"Ben Bacarisse" <ben.usenet@bsb.me.uk> wrote in message
news:0.6a8ba340e934c61ead30.20120622231916BST.87wr2zm1ln.fsf@bsb.me.uk...
> "BartC" <bc@freeuk.com> writes:
>>>>>> result of i=(++i)+(++i)+(++i), when i starts at 0
>> Maybe the OP in the original thread had a Java background. Or maybe he
>> just
>> decided the common-sense result was 6, and wanted to know why C didn't
>> agree.
> But you know C's rules, so why did say the value *ought* to be 6. What
> was that about?
But I said 'ought', not 'should'. The '6' was the value that might
reasonably have been expected, *before* it was put through C. I wasn't
suggesting the C language was doing anything wrong by not giving that
answer.
It was a reasonable expectation, because put through any simplistically
generated code, that what's most likely to come out. This was a beginner,
and likely his/her thought processes were similar to mine when attempting to
valuate the expression on it's own merits.
As an example of it's kind, it was less convoluted than some of them, and
didn't depend so much on evaluation order.
A beginner could equally have asked why:
unsigned int a,b,c;
a=2000000000;
b=3000000000;
c=a+b;
printf("%u\n", c);
gives the answer 705032704, rather than 5 billion. And I might have said:
"I agree, it ought to be 5000000000" before going on to explain why it gives
what is apparently the wrong answer.
> Did you want to re-start the debate about the sequence
> point rules? Did you want to propose a new semantics for C expressions?
No and No.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-06-22 17:44 +0100 |
| Message-ID | <0.634ee044a690c5ec95a1.20120622174414BST.878vffnvoh.fsf@bsb.me.uk> |
| In reply to | #1849 |
"BartC" <bc@freeuk.com> writes: Taking about the undefined C statement: i = ++i + ++i + ++i; <snip> > (Sorry to drag the discussion here, but it was becoming uncomfortable in > comp.lang.c) The original post and your reply was about C. My rely only makes sense there. I don't think I have anything to say about the matter in comp.programming, other than the rather obvious fact that meaning, if any, depends on the language. -- Ben.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.programming
csiph-web