Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 06-21-2012, 07:56 PM
Yogesh Yadav Pacheria
Guest
 
Posts: n/a
Default Pre And Post Increment Operator Output

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

Thanks in Advance
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 06-21-2012, 08:08 PM
Paul N
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

On Jun 21, 8:56*pm, Yogesh Yadav Pacheria <yogeshpache...@gmail.com>
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
>
> Thanks in Advance


The FAQ is at http://c-faq.com/ . Read it, you'll learn a lot from it.
Reply With Quote
  #3 (permalink)  
Old 06-21-2012, 08:11 PM
John Gordon
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

In <8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com> Yogesh Yadav Pacheria <yogeshpacheria@gmail.com> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Reply With Quote
  #4 (permalink)  
Old 06-21-2012, 08:29 PM
Andrew Cooper
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

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
>
> Thanks in Advance


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
Reply With Quote
  #5 (permalink)  
Old 06-21-2012, 08:52 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

On 6/21/2012 4:08 PM, Paul N wrote:
> On Jun 21, 8:56 pm, Yogesh Yadav Pacheria <yogeshpache...@gmail.com>
> 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
>>
>> Thanks in Advance

>
> 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
to your issue, too.


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


Reply With Quote
  #6 (permalink)  
Old 06-21-2012, 09:08 PM
BartC
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output



"Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> wrote in message
news:8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com...
> 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
ever did anything useful to start with..).

--
Bartc

Reply With Quote
  #7 (permalink)  
Old 06-21-2012, 09:32 PM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

On 6/21/2012 5:08 PM, BartC wrote:
>
>
> "Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> wrote in message
> news:8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com...
>> 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
esosman@ieee-dot-org.invalid


Reply With Quote
  #8 (permalink)  
Old 06-21-2012, 10:17 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

Yogesh Yadav Pacheria <yogeshpacheria@gmail.com> 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) kst-u@mib.org <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"
Reply With Quote
  #9 (permalink)  
Old 06-21-2012, 11:00 PM
BartC
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output



"Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
news:js03uf$sb3$1@dont-email.me...
> On 6/21/2012 5:08 PM, BartC wrote:


>> "Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> wrote in message
>> news:8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com...


>>> 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

Reply With Quote
  #10 (permalink)  
Old 06-21-2012, 11:51 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

"BartC" <bc@freeuk.com> writes:
> "Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
> news:js03uf$sb3$1@dont-email.me...
>> On 6/21/2012 5:08 PM, BartC wrote:

>
>>> "Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> wrote in message
>>> news:8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com...

>
>>>> 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) kst-u@mib.org <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"
Reply With Quote
  #11 (permalink)  
Old 06-22-2012, 01:06 AM
Eric Sosman
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

On 6/21/2012 7:00 PM, BartC wrote:
>
>
> "Eric Sosman" <esosman@ieee-dot-org.invalid> wrote in message
> news:js03uf$sb3$1@dont-email.me...
>> On 6/21/2012 5:08 PM, BartC wrote:

>
>>> "Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> wrote in message
>>> news:8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com...

>
>>>> 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?


Nitwit.

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


Reply With Quote
  #12 (permalink)  
Old 06-22-2012, 04:03 AM
Yogesh Yadav Pacheria
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

I am new to see and wanna learn it deeply

Could u guyz tell me some good books or web links
Reply With Quote
  #13 (permalink)  
Old 06-22-2012, 07:06 AM
nick_keighley_nospam@hotmail.com
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

On Friday, June 22, 2012 5:03:42 AM UTC+1, Yogesh Yadav Pacheria wrote:

> I am new to see and wanna learn it deeply
>
> Could u guyz tell me some good books or web links


"The C Programming Language" Kernighan and Ritchie (known as K&R).

Reply With Quote
  #14 (permalink)  
Old 06-22-2012, 07:30 AM
Mark Bluemel
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

On 22/06/2012 05:03, Yogesh Yadav Pacheria wrote:
> I am new to see and wanna learn it deeply


Hmmm... And your first question to the group consists of a rather
convoluted and contrived example of pre- and post- increments.

> Could u guyz tell me some good books or web links


If you're serious (and I have some doubts) section 18 of the FAQ
(http://c-faq.com) has pointers to some resources.


Reply With Quote
  #15 (permalink)  
Old 06-22-2012, 08:27 AM
Heinrich Wolf
Guest
 
Posts: n/a
Default Re: Pre And Post Increment Operator Output

"Yogesh Yadav Pacheria" <yogeshpacheria@gmail.com> schrieb im Newsbeitrag
news:8404c4f1-a3b7-46b7-ad9c-17de265f9aee@googlegroups.com...
>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
>
> Thanks in Advance


Hi,

I find it unreasonable to use such code in serious projects.
My old Turbo C 2.0 prints 9 and so does my Borland C++ Builder 5.
I do not care why.

kind regards
Heiner

Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 06:05 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.