Pre And Post Increment Operator Output

Discussion in 'C Programming' started by Yogesh Yadav Pacheria, Jun 21, 2012.

  1. 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
     
    Yogesh Yadav Pacheria, Jun 21, 2012
    #1
    1. Advertising

  2. Yogesh Yadav Pacheria

    Paul N Guest

    On Jun 21, 8:56 pm, 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


    The FAQ is at http://c-faq.com/ . Read it, you'll learn a lot from it.
     
    Paul N, Jun 21, 2012
    #2
    1. Advertising

  3. Yogesh Yadav Pacheria

    John Gordon Guest

    In <> Yogesh Yadav Pacheria <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
     
    John Gordon, Jun 21, 2012
    #3
  4. 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
     
    Andrew Cooper, Jun 21, 2012
    #4
  5. Yogesh Yadav Pacheria

    Eric Sosman Guest

    On 6/21/2012 4:08 PM, Paul N wrote:
    > On Jun 21, 8:56 pm, 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

    >
    > 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
    d
     
    Eric Sosman, Jun 21, 2012
    #5
  6. Yogesh Yadav Pacheria

    BartC Guest

    "Yogesh Yadav Pacheria" <> wrote in message
    news:...
    > 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
     
    BartC, Jun 21, 2012
    #6
  7. Yogesh Yadav Pacheria

    Eric Sosman Guest

    On 6/21/2012 5:08 PM, BartC wrote:
    >
    >
    > "Yogesh Yadav Pacheria" <> wrote in message
    > news:...
    >> 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
    d
     
    Eric Sosman, Jun 21, 2012
    #7
  8. Yogesh Yadav Pacheria <> 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) <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"
     
    Keith Thompson, Jun 21, 2012
    #8
  9. Yogesh Yadav Pacheria

    BartC Guest

    "Eric Sosman" <> wrote in message
    news:js03uf$sb3$...
    > On 6/21/2012 5:08 PM, BartC wrote:


    >> "Yogesh Yadav Pacheria" <> wrote in message
    >> news:...


    >>> 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
     
    BartC, Jun 22, 2012
    #9
  10. "BartC" <> writes:
    > "Eric Sosman" <> wrote in message
    > news:js03uf$sb3$...
    >> On 6/21/2012 5:08 PM, BartC wrote:

    >
    >>> "Yogesh Yadav Pacheria" <> wrote in message
    >>> news:...

    >
    >>>> 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) <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"
     
    Keith Thompson, Jun 22, 2012
    #10
  11. Yogesh Yadav Pacheria

    Eric Sosman Guest

    On 6/21/2012 7:00 PM, BartC wrote:
    >
    >
    > "Eric Sosman" <> wrote in message
    > news:js03uf$sb3$...
    >> On 6/21/2012 5:08 PM, BartC wrote:

    >
    >>> "Yogesh Yadav Pacheria" <> wrote in message
    >>> news:...

    >
    >>>> 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
    d
     
    Eric Sosman, Jun 22, 2012
    #11
  12. I am new to see and wanna learn it deeply

    Could u guyz tell me some good books or web links
     
    Yogesh Yadav Pacheria, Jun 22, 2012
    #12
  13. Yogesh Yadav Pacheria

    Guest

    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).
     
    , Jun 22, 2012
    #13
  14. Yogesh Yadav Pacheria

    Mark Bluemel Guest

    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.
     
    Mark Bluemel, Jun 22, 2012
    #14
  15. "Yogesh Yadav Pacheria" <> schrieb im Newsbeitrag
    news:...
    >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
     
    Heinrich Wolf, Jun 22, 2012
    #15
  16. Yogesh Yadav Pacheria

    BartC Guest

    "Keith Thompson" <> wrote in message
    news:...
    > "BartC" <> writes:
    >>> On 6/21/2012 5:08 PM, BartC wrote:

    >>
    >>>> "Yogesh Yadav Pacheria" <> wrote in message


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


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


    OK, so we fix it so that the result is ....? Any guesses?

    Changing the erroneous code so that it stays within the rules:

    int i = 0;
    int t1,t2,t3;

    t1=++i;
    t2=++i;
    t3=++i;
    i=t1+t2+t3;
    printf("%d",i);

    Now gives a valid result. Which happens to coincide with the OP's
    expectations, and with what I said it *ought* to be (for which I was called
    a 'nitwit', even though I went on to explain why the OP wasn't getting that
    result), and in fact with what most of my compilers were already giving me
    anyway; presumably they were already using code along the same lines!)

    However, the interesting question is, why anyone - outside c.l.c obviously -
    might think the output of the OP's original code might be 6, rather than any
    other number (or any other manifestation, according to previous threads
    along the same lines).

    Or, are we not allowed to think or guess anything at all, simply because the
    C standard says the original expression has undefined behaviour?

    --
    Bartc
     
    BartC, Jun 22, 2012
    #16
  17. Yogesh Yadav Pacheria

    Ian Collins Guest

    On 06/22/12 07:56 AM, 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


    Is this question auto-generated by a cron job somewhere?

    --
    Ian Collins
     
    Ian Collins, Jun 22, 2012
    #17
  18. Yogesh Yadav Pacheria

    Mark Bluemel Guest

    On 22/06/2012 12:11, Ian Collins wrote:
    > On 06/22/12 07:56 AM, 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

    >
    > Is this question auto-generated by a cron job somewhere?


    With a set of Lex scripts to generate varying English syntax? I note
    that the Jive/Valley Girl/Swedish Chef/Pig Latin translator is available
    on the web - someone must have a txtspk version...
     
    Mark Bluemel, Jun 22, 2012
    #18
  19. Yogesh Yadav Pacheria

    Noob Guest

    Eric Sosman wrote:

    > Nitwit.


    Intentionally or not, a large number of BartC's posts are troll-ish.
    For this reason, I send his messages to the bit bucket.
     
    Noob, Jun 22, 2012
    #19
  20. Yogesh Yadav Pacheria

    BartC Guest

    Re: BartC (was: Pre And Post Increment Operator Output)

    "Noob" <root@127.0.0.1> wrote in message news:js1l0j$efm$...

    > Intentionally or not, a large number of BartC's posts are troll-ish.
    > For this reason, I send his messages to the bit bucket.


    For the information of anybody else, my posts are not intentionally anything
    of the kind.

    My first post in a thread might sometimes offer a different point of view,
    or a more casual approach, but quite often that gets picked up, and I feel
    obliged to defend my position.

    Let me apologise here to all those who've had to read all my nonsense in the
    past.

    --
    Bart. C.
     
    BartC, Jun 22, 2012
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Sheriff
    Replies:
    10
    Views:
    726
    Jack Klein
    Sep 25, 2004
  2. Replies:
    10
    Views:
    45,054
    gene tani
    Jan 13, 2006
  3. Replies:
    8
    Views:
    932
    Alf P. Steinbach
    Nov 22, 2007
  4. Alf P. Steinbach /Usenet
    Replies:
    0
    Views:
    899
    Alf P. Steinbach /Usenet
    May 22, 2011
  5. Peng Yu

    post increment or pre increment?

    Peng Yu, Nov 21, 2008, in forum: Perl Misc
    Replies:
    7
    Views:
    529
    Peter J. Holzer
    Nov 23, 2008
Loading...

Share This Page