value of k = ++i + i++ * ++i;
Author Message
value of k = ++i + i++ * ++i;

Hello - consider the following program:

void main()
{
int i=2, k;
k =  ++i + i++ * ++i;
printf("i = %d, k = %d \n",i,k);

Quote:
}

It prints out:
i = 5, k =20

Can someone please explain to me why the value of k is 20??
post increment operations, but cannot seem to derive the above values.

much thanks.

Tue, 23 Nov 2004 10:07:03 GMT
value of k = ++i + i++ * ++i;

Quote:
> Hello - consider the following program:

> void main()
> {
>     int i=2, k;
>     k =  ++i + i++ * ++i;
>     printf("i = %d, k = %d \n",i,k);
> }

> It prints out:
> i = 5, k =20

> Can someone please explain to me why the value of k is 20??
> I already searched through google on explanations for the pre versus
> post increment operations, but cannot seem to derive the above values.

> much thanks.

Guess what? You're in "undefined behavior" territory! Different compilers
can produce different results since there's no sequence point between the
increments and you reference the value of i!

Gregory Pietsch

Tue, 23 Nov 2004 10:16:10 GMT
value of k = ++i + i++ * ++i;
Submitted by "EG" to comp.lang.c:

Quote:
> Hello - consider the following program:

> void main()
> {
>     int i=2, k;
>     k =  ++i + i++ * ++i;
>     printf("i = %d, k = %d \n",i,k);
> }

> It prints out:
> i = 5, k =20

> Can someone please explain to me why the value of k is 20??
> I already searched through google on explanations for the pre versus
> post increment operations, but cannot seem to derive the above values.

> much thanks.

See http://www.eskimo.com/~scs/C-faq/q3.2.html

And main should always be int... and you need <stdio.h> for
printf.

--
Andreas K?h?ri
Bioinformatics and Unix System Support, Dunedin, New Zealand
--------------------------------------------------------------

Tue, 23 Nov 2004 10:20:41 GMT
value of k = ++i + i++ * ++i;

Quote:

>Hello - consider the following program:

>void main()
>{
>    int i=2, k;
>    k =  ++i + i++ * ++i;
>    printf("i = %d, k = %d \n",i,k);
>}

>It prints out:
>i = 5, k =20

>Can someone please explain to me why the value of k is 20??
>post increment operations, but cannot seem to derive the above values.

Because you're invoking undefined behavior by modifying i multiple times
between sequence points, so the compiler is allowed to generate code
that does anything it wants it to do (or even no code at all, though
that appears to not be the case here).

dave

--

[T]ry thinking someday. It might hurt a little at first, but you'll
--Joona I Palaste roasts a troll in comp.lang.c

Tue, 23 Nov 2004 10:11:41 GMT
value of k = ++i + i++ * ++i;

Quote:
>Hello - consider the following program:

>void main()
>{
>    int i=2, k;
>    k =  ++i + i++ * ++i;
>    printf("i = %d, k = %d \n",i,k);
>}

>It prints out:
>i = 5, k =20

>Can someone please explain to me why the value of k is 20??
>post increment operations, but cannot seem to derive the above values.

>much thanks.

Check the FAQ (http://www.eskimo.com/~scs/C-faq/top.html), section 3.

<<Remove the del for email>>

Tue, 23 Nov 2004 10:32:13 GMT
value of k = ++i + i++ * ++i;

comp.lang.c:

Quote:
> Hello - consider the following program:

> void main()

After the line above the C language standard places no requirements
whatsoever on your program, so whatever it does is just as right or
wrong as anything else.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq

Tue, 23 Nov 2004 10:44:27 GMT
value of k = ++i + i++ * ++i;

Quote:

> Hello - consider the following

It's not a program.  Your various illegal constructions or those with
undefined behavior are covered in the FAQ.  Please check it before
posting.  That's what civilized people do.

Quote:

> void main()
> {
>     int i=2, k;
>     k =  ++i + i++ * ++i;
>     printf("i = %d, k = %d \n",i,k);
> }

> It prints out:
> i = 5, k =20

> Can someone please explain to me why the value of k is 20??

Your program could print '82' or 'your mother is a troll' and still
you would have no cause for complaint.

Quote:
> I already searched through google on explanations for the pre versus
> post increment operations, but cannot seem to derive the above values.

You did not check the FAQ, or the similar questions from other clueless
idiots to this newsgroup.

Tue, 23 Nov 2004 15:08:03 GMT
value of k = ++i + i++ * ++i;

Quote:
> Your program could print '82' or 'your mother is a troll' and still
> you would have no cause for complaint.

That sounds like pretty serious stuff. So you are saying that printf() will
malfunction because of the value the integer it is passed, or that because
function printf() is called from does return (or not return) a specific
value or type, or because the order the expression in question is executed
is undefined? Regardless of the order the expression is parsed, undefined or
not, it should yield integer passed to printf() should yield correct
behaviour of printf(), unless, the whole langauge is steaming pile of shit
(*).. or that what you are saying is just the correct void main() -anti
rhyme.

Besides, if the question was so idiotic and so and so forth I recommend you
ignore such questions in the future.

(*) I am sort of fond of C so I think what you are saying is is just a
knee-jerk reaction.

Tue, 23 Nov 2004 16:16:08 GMT
value of k = ++i + i++ * ++i;
On Fri, 7 Jun 2002 11:16:08 +0300, Seppo Citymarket said:

Quote:
>> Your program could print '82' or 'your mother is a troll' and still
>> you would have no cause for complaint.
> That sounds like pretty serious stuff. So you are saying that printf() will
> malfunction because of the value the integer it is passed

No - your code (by breaking a "must" rule) renders the entire
program unreliable. By doing something the behaviour of which is
undefined, the compiler is free to interpret the program in it's
entirety as doing something completely different.

As an example, if a compiler (in the corse of a compilation)
discovered "i=i++;" in the program, it could replace the entire
executable with "hello, world". There's nothing syntactically
wrong with the code, so it must compile. But it's behaviour on
running is completely unpredictable. It may, by some miracle,
even do what you expect.

Quote:
> Regardless of the order the expression is parsed, undefined or
> not, it should yield integer passed to printf() should yield correct
> behaviour of printf(), unless, the whole langauge is steaming pile of shit

But the void main() and the i++ + i++ things result in undefined
behaviour, which I've explained above. The "demons flying out of
the nose" example is a little extreme - the point is that after
doing something undefined, the program is no longer trustworthy,
in any aspect.

Quote:
> Besides, if the question was so idiotic and so and so forth I recommend you
> ignore such questions in the future.

It *is* a frequently asked question. As such it's in the FAQ
list. And being in the FAQ list, such questions occasionally get
treated harshly :)

Cheers,
Dave.

--
David Neary,
E-Mail: bolsh at gimp dot org
CV: http://www.redbrick.dcu.ie/~bolsh/CV/CV.html

Tue, 23 Nov 2004 16:36:58 GMT
value of k = ++i + i++ * ++i;

Quote:
> > Your program could print '82' or 'your mother is a troll' and still
> > you would have no cause for complaint.

> That sounds like pretty serious stuff.

Yep.

Quote:
> So you are saying that printf() will
> malfunction because of the value the integer it is passed, or that because
> function printf() is called from does return (or not return) a specific
> value or type, or because the order the expression in question is executed
> is undefined?

No, it can totally malfunction because printf() is a variadic function and
variadic functions are not required to function properly in the absence of a
prototype. On some systems this proviso allows variadic functions to have a
different machine code calling standard to normal ones, so it definitely
matters.

On top of that it's an error in C99 to call any function without it being
declared.

--
Kevin Bracey
http://www.bracey-griffith.freeserve.co.uk/

Tue, 23 Nov 2004 17:50:55 GMT
value of k = ++i + i++ * ++i;

Quote:
>> Your program could print '82' or 'your mother is a troll' and still
>> you would have no cause for complaint.

> That sounds like pretty serious stuff. So you are saying that printf() will
> malfunction because of the value the integer it is passed, or that because
> function printf() is called from does return (or not return) a specific
> value or type, or because the order the expression in question is executed
> is undefined?

No, he's saying that Standard specifically places *no constraint whatsoever*
on the behaviour of `k =  ++i + i++ * ++i;`. An implementation can do
whatever it likes. The code is broken.

I suspect that attempts to restrict the interpretation of such code
to "sensible" meanings, while still allowing optimising compilers freedom
to rearrange, would generate long, complicated, detail-ridden specs
which would confuse users and implementors alike. Better to say "this
doesn't mean anything - don't do it" and rely on quality-of-implementation
to keep compilers sane.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html

Tue, 23 Nov 2004 17:47:44 GMT
value of k = ++i + i++ * ++i;

Quote:

> > Your program could print '82' or 'your mother is a troll' and still
> > you would have no cause for complaint.

> That sounds like pretty serious stuff. So you are saying that printf() will
> malfunction because of the value the integer it is passed,

No, he's not saying printf will malfunction. He is saying your program
invokes undefined behaviour. It is your program which is malfunctioning,
not printf.

Quote:
> or that because
> function printf() is called from does return (or not return) a specific
> value or type, or because the order the expression in question is executed
> is undefined? Regardless of the order the expression is parsed, undefined or
> not, it should yield integer passed to printf() should yield correct
> behaviour of printf(),

No, you don't understand "undefined behaviour". Look up the two words in
a dictionary.

Quote:
> unless, the whole langauge is steaming pile of shit

Not the language, just your program.

Quote:
> (*).. or that what you are saying is just the correct void main() -anti
> rhyme.

Yes, we're just being correct. If you prefer incorrectness, try a
different newsgroup.

<snip>

--

"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Tue, 23 Nov 2004 18:11:48 GMT
value of k = ++i + i++ * ++i;

Quote:

>> Your program could print '82' or 'your mother is a troll' and still
>> you would have no cause for complaint.

> That sounds like pretty serious stuff.

Yup.  Gcc once started a game (nethack or rogue, I think), just to make
a point, if you invoked some kinds of undefined behaviour.  Pretty clear
way to say "don't do that!":-)

Quote:
> So you are saying that printf() will malfunction because of the value
> the integer it is passed, or (...)

It *may*, not *will*, malfunction.  One possible reason: The
implementation could pass the arguments to varargs functions in a
different way (different registers, or on the stack, or something) from
how it passes arguments to non-varargs functions.

--
Hallvard

Tue, 23 Nov 2004 18:20:24 GMT
value of k = ++i + i++ * ++i;

Quote:

>>> Your program could print '82' or 'your mother is a troll' and still
>>> you would have no cause for complaint.

>> That sounds like pretty serious stuff. So you are saying that printf() will
>> malfunction because of the value the integer it is passed,

> No, he's not saying printf will malfunction. He is saying your program
> invokes undefined behaviour. It is your program which is malfunctioning,
> not printf.

Wrong attributions.  ``Seppo Citymarket'' is not the OP.

Quote:
>> or that because
>> function printf() is called from does return (or not return) a specific
>> value or type, or because the order the expression in question is executed
>> is undefined? Regardless of the order the expression is parsed, undefined or
>> not, it should yield integer passed to printf() should yield correct
>> behaviour of printf(),

> No, you don't understand "undefined behaviour". Look up the two words in
> a dictionary.

Bogus advice.  You cannot replace the C Standard with an
arbitrary dictionary.

Tak-Shing

Tue, 23 Nov 2004 18:58:50 GMT
value of k = ++i + i++ * ++i;

Quote:

>  the question was so idiotic and so and so forth

After a question goes into the FAQ,
it becomes inappropriate to ask it in the newsgroup,
unless you're actually questioning the accuracy of the FAQ.

You've read this newsgroup enough times to know that.

--
pete

Tue, 23 Nov 2004 19:18:26 GMT

 Page 1 of 2 [ 25 post ] Go to page: [1] [2]

Relevant Pages
 5. i = i++; 7. i++? 11. i++