evaluate it

Discussion in 'C++' started by ashim.seth@gmail.com, Jul 5, 2005.

  1. Guest

    hey people i was just testin this:
    int i=5,j;
    j=i++ + ++i;
    cout<<j;


    and then...

    int i=5;
    int j= i++ + ++i;
    cout<<j;


    both givin different results
    y?
    plz help
    , Jul 5, 2005
    #1
    1. Advertising

  2. wrote:
    > hey people i was just testin this:
    > int i=5,j;
    > j=i++ + ++i;
    > cout<<j;
    >
    >
    > and then...
    >
    > int i=5;
    > int j= i++ + ++i;
    > cout<<j;
    >
    >
    > both givin different results
    > y?
    > plz help


    Read the FAQ, section 39

    V
    Victor Bazarov, Jul 5, 2005
    #2
    1. Advertising

  3. Peter Julian Guest

    <> wrote in message
    news:...
    > hey people i was just testin this:
    > int i=5,j;
    > j=i++ + ++i;
    > cout<<j;
    >
    >
    > and then...
    >
    > int i=5;
    > int j= i++ + ++i;
    > cout<<j;
    >
    >
    > both givin different results
    > y?
    > plz help
    >


    The results are irrelevent. Since you are refering to the same variable in
    that compound statement, the standard does not guarentee when, how or
    whether the variable i gets incremented. In fact, the standard has a
    requirement that the said variable be only modified once in between sequence
    points.

    Try:

    #include <iostream>

    int main()
    {
    int i = 5;

    std::cout << "i = " << i << "\n";

    int j(i++);

    std::cout << "i = " << i << "\n";
    std::cout << "j = " << j << "\n";

    j += (++i);

    std::cout << "i = " << i << "\n";
    std::cout << "j = " << j << "\n";

    return 0;
    }

    /*
    i = 5
    i = 6
    j = 5
    i = 7
    j = 12
    */
    Peter Julian, Jul 5, 2005
    #3
    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. senthil
    Replies:
    0
    Views:
    491
    senthil
    Mar 4, 2004
  2. Brad Smith
    Replies:
    2
    Views:
    481
    Blaise Pascal Tine
    Jun 27, 2003
  3. Yuriy

    How to evaluate control

    Yuriy, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    305
    Yuriy
    Aug 12, 2003
  4. John Boghossian

    Evaluate contenttype

    John Boghossian, Nov 28, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    351
    Jacob Yang [MSFT]
    Dec 4, 2003
  5. darrel
    Replies:
    8
    Views:
    439
    Michael Ramey
    Feb 11, 2004
Loading...

Share This Page