Velocity Reviews > Pre And Post Increment Operator Output

# Pre And Post Increment Operator Output

Guest
Posts: n/a

 06-21-2012
I tried this Code

int main()
{
int i = 0;
i = ++i + ++i + ++i;
printf("%d",i);
}

Output should be 6

But In GCC it's 7

How and plz tell the order of evalution of pre and post bot

Paul N
Guest
Posts: n/a

 06-21-2012
On Jun 21, 8:56*pm, Yogesh Yadav Pacheria <(E-Mail Removed)>
wrote:
> I tried this Code
>
> int main()
> {
> * *int i = 0;
> * *i = ++i + ++i + ++i;
> * *printf("%d",i);
>
> }
>
> Output should be 6
>
> But In GCC it's 7
>
> How and plz tell the order of evalution of pre and post bot
>

The FAQ is at http://c-faq.com/ . Read it, you'll learn a lot from it.

John Gordon
Guest
Posts: n/a

 06-21-2012
In <(E-Mail Removed)> Yogesh Yadav Pacheria <(E-Mail Removed)> writes:

> int main()
> {
> int i = 0;
> i = ++i + ++i + ++i;
> printf("%d",i);
> }

> Output should be 6

No it shouldn't. Using multiple increment operators in this way is
undefined.

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Andrew Cooper
Guest
Posts: n/a

 06-21-2012
On 21/06/2012 20:56, Yogesh Yadav Pacheria wrote:
> I tried this Code
>
> int main()
> {
> int i = 0;
> i = ++i + ++i + ++i;
> printf("%d",i);
> }
>
> Output should be 6
>
> But In GCC it's 7
>
> How and plz tell the order of evalution of pre and post bot
>

You have provided a program which tries to update i more than once in a
sequence point. This is strictly undefined by the C spec, so the
compiler is free to interpret it how it likes.

The disassembly (from GCC 4.6.3) looks a little like:

movl \$7, %esi
movl \$.LC0, %edi # String "%d\n"
movl \$0, %eax
call printf

So what happens is that GCC calculates the value of i at compile time
and sticks it in as a constant.

~Andrew

Eric Sosman
Guest
Posts: n/a

 06-21-2012
On 6/21/2012 4:08 PM, Paul N wrote:
> On Jun 21, 8:56 pm, Yogesh Yadav Pacheria <(E-Mail Removed)>
> wrote:
>> I tried this Code
>>
>> int main()
>> {
>> int i = 0;
>> i = ++i + ++i + ++i;
>> printf("%d",i);
>>
>> }
>>
>> Output should be 6
>>
>> But In GCC it's 7
>>
>> How and plz tell the order of evalution of pre and post bot
>>

>
> The FAQ is at http://c-faq.com/ . Read it, you'll learn a lot from it.

Pay special attention to Question 3.2, which is almost
exactly what you asked. Much of the rest of Section 3 pertains

--
Eric Sosman
(E-Mail Removed)d

BartC
Guest
Posts: n/a

 06-21-2012

"Yogesh Yadav Pacheria" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I tried this Code
>
> int main()
> {
> int i = 0;
> i = ++i + ++i + ++i;
> printf("%d",i);
> }
>
> Output should be 6
>
> But In GCC it's 7
>
> How and plz tell the order of evalution of pre and post bot

I agree it ought to be 6. Other C compilers, and even one or two other
languages, give a result of 6.

But C allows compilers to evaluate expressions their own way, which may give
undefined results when a variable is modified several times in the same
expression. GCC must be one of those compilers that takes full advantage of
that.

So you will have to rearrange the expression into separate statements (if it

--
Bartc

Eric Sosman
Guest
Posts: n/a

 06-21-2012
On 6/21/2012 5:08 PM, BartC wrote:
>
>
> "Yogesh Yadav Pacheria" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> I tried this Code
>>
>> int main()
>> {
>> int i = 0;
>> i = ++i + ++i + ++i;
>> printf("%d",i);
>> }
>>
>> Output should be 6
>>
>> But In GCC it's 7
>>
>> How and plz tell the order of evalution of pre and post bot

>
> I agree it ought to be 6.

Nitwit.

--
Eric Sosman
(E-Mail Removed)d

Keith Thompson
Guest
Posts: n/a

 06-21-2012
Yogesh Yadav Pacheria <(E-Mail Removed)> writes:
> I tried this Code
>
> int main()
> {
> int i = 0;
> i = ++i + ++i + ++i;
> printf("%d",i);
> }
>
> Output should be 6
>
> But In GCC it's 7
>
> How and plz tell the order of evalution of pre and post bot

You've already been directed to the C FAQ, particularly section 3, which
explains why your assumption that the output should be 6 is meaningless.

There are some other (fairly) minor problems with your code:

You need "#include <stdio.h>" in any program that calls printf.

"int main()" should be "int main(void)".

You should print a newline at the end of the output:

printf("%d\n", i);

You should have a "return 0;" at the end of the program. It's not
necessary if your compiler conforms to C99 or later, but it's a good
idea.

The real problem is this: whatever

i = ++i + ++i + ++i;

was *intended* to do, there is certainly a better and clearer way to
write it -- even its behavior were well defined (as it is in some
languages).

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

BartC
Guest
Posts: n/a

 06-21-2012

"Eric Sosman" <(E-Mail Removed)> wrote in message
news:js03uf\$sb3\$(E-Mail Removed)...
> On 6/21/2012 5:08 PM, BartC wrote:

>> "Yogesh Yadav Pacheria" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...

>>> int i = 0;
>>> i = ++i + ++i + ++i;
>>> printf("%d",i);

>>> Output should be 6
>>>
>>> But In GCC it's 7

>> I agree it ought to be 6.

>
> Nitwit.

Yet, given the OP's program, and a randomly assigned but unknown compiler,
what result would you put your money on, if you were obliged to gamble?

And once you know the result, would you gamble on the same compiler giving
the exactly the same answer one more time?

--
Bartc

Keith Thompson
Guest
Posts: n/a

 06-21-2012
"BartC" <(E-Mail Removed)> writes:
> "Eric Sosman" <(E-Mail Removed)> wrote in message
> news:js03uf\$sb3\$(E-Mail Removed)...
>> On 6/21/2012 5:08 PM, BartC wrote:

>
>>> "Yogesh Yadav Pacheria" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...

>
>>>> int i = 0;
>>>> i = ++i + ++i + ++i;
>>>> printf("%d",i);

>
>>>> Output should be 6
>>>>
>>>> But In GCC it's 7

>
>>> I agree it ought to be 6.

>>
>> Nitwit.

>
> Yet, given the OP's program, and a randomly assigned but unknown compiler,
> what result would you put your money on, if you were obliged to gamble?

Fortunately, we are not obliged to gamble.

> And once you know the result, would you gamble on the same compiler giving
> the exactly the same answer one more time?

What would be the point? Don't waste time guessing what the code might
do for a given implementation. Just fix it.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"