CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38
  1. #1
    Join Date
    Aug 2000
    Posts
    1,471

    What is the Output of this program?

    void main()

    {

    int var1 = 10;

    cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;

    }

    And why? Thanks for your inputs.

  2. #2
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: What is the Output of this program?

    Undefined due to the errors.
    Last edited by Graham; June 22nd, 2005 at 01:55 PM.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  3. #3
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: What is the Output of this program?

    Quote Originally Posted by dullboy
    void main()

    {

    int var1 = 10;

    cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;

    }

    And why? Thanks for your inputs.
    Maybe I should expand on that:

    1. it's int main(), not void main()
    2. you cannot modify the stored value of a variable more than once between sequence points. This is what the "cout" line is doing. The behaviour is undefined, therefore the output of the program cannot be predicted.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  4. #4
    Join Date
    May 2004
    Location
    Michigan, United States
    Posts
    457

    Re: What is the Output of this program?

    The output is:
    'cout' : undeclared identifier

    because you forgot to include <iostream.h>


    Seriously, though, it should be:
    13, 12, 12, 10

    Starting at the right, the variable value is printed, then incremented. So, "10" goes to cout (eventually). Then it is incremented to 11.

    The increment that occurs to the left of this happens before the value is sent to cout, so it is incremented to 12, then printed.

    Similarly with the last (err, first) two.
    Programming today is a race between software engineers striving to build bigger and better idiot-proof programs and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

  5. #5
    Join Date
    Aug 2000
    Posts
    1,471

    Re: What is the Output of this program?

    Thanks for your reply. But if you run this program, output would be "13 12 12 10". I don't understand the output.
    Quote Originally Posted by Graham
    Maybe I should expand on that:

    1. it's int main(), not void main()
    2. you cannot modify the stored value of a variable more than once between sequence points. This is what the "cout" line is doing. The behaviour is undefined, therefore the output of the program cannot be predicted.

  6. #6
    Join Date
    May 2004
    Location
    Norway
    Posts
    655

    Re: What is the Output of this program?

    Quote Originally Posted by Bond
    because you forgot to include <iostream.h>
    Scratch the .h and we agree.
    Quote Originally Posted by Bond
    Starting at the right,
    There is no guarantee that the right expression will be evaluated first, so the results are, as Graham correctly pointed out, undefined.
    Insert entertaining phrase here

  7. #7
    Join Date
    Aug 2000
    Posts
    1,471

    Re: What is the Output of this program?

    But my question is that why it is starting at the right instead of left? For example, int x = 1; int y = 2; cout<<x<<y; Then the output is 1 2 instead of 2 1.
    Quote Originally Posted by Bond
    The output is:
    'cout' : undeclared identifier

    because you forgot to include <iostream.h>


    Seriously, though, it should be:
    13, 12, 12, 10

    Starting at the right, the variable value is printed, then incremented. So, "10" goes to cout (eventually). Then it is incremented to 11.

    The increment that occurs to the left of this happens before the value is sent to cout, so it is incremented to 12, then printed.

    Similarly with the last (err, first) two.

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What is the Output of this program?

    Quote Originally Posted by dullboy
    Thanks for your reply. But if you run this program, output would be "13 12 12 10". I don't understand the output.
    Don't try to understand the output. If you ran this on another compiler, in all likelihood the output is different. That is what is meant by "undefined behavior".

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Aug 2000
    Posts
    1,471

    Re: What is the Output of this program?

    Even you are right, there is always logics behind the output, which I try to understand.
    Quote Originally Posted by Paul McKenzie
    Don't try to understand the output. If you ran this on another compiler, in all likelihood the output is different. That is what is meant by "undefined behavior".

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What is the Output of this program?

    Quote Originally Posted by dullboy
    Even you are right, there is always logics behind the output, which I try to understand.
    Why not concentrate on learning how not to write code like this, and concentrate on proper C++ coding techniques?

    There is no "logic" behind the output. Once you introduce undefined behavior, then anything under the hood could be happening. The answer can be 1,489,423,324 for all it's worth. Again, the behavior is undefined.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; June 22nd, 2005 at 02:40 PM.

  11. #11
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: What is the Output of this program?

    operator<< is a function, the order of evaluation for function arguments are undefined: http://www.research.att.com/~bs/bs_f...aluation-order

    If you want to know why your compiler is evaluating function arguments from right to left or vice-versa, ask your compiler vendor.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

  12. #12
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: What is the Output of this program?

    Look, it's quite simple: you are not allowed to modify the stored value of a variable more than once between sequence points. This program attempts to modify it three times between sequence points. Order of evaluation of arguments is totally irrelevant - it's an illegal statement.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  13. #13
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: What is the Output of this program?

    And just in case it needs hammering home, here's the relevant paragraph from the standard (Section 5, para 4):
    Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  14. #14
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: What is the Output of this program?

    Quote Originally Posted by cma
    operator<< is a function, the order of evaluation for function arguments are undefined: http://www.research.att.com/~bs/bs_f...aluation-order

    If you want to know why your compiler is evaluating function arguments from right to left or vice-versa, ask your compiler vendor.
    Yes, operator<<() is a function, but something like

    Code:
    cout << a << b;
    is not one function call. First, the compiler looks at

    cout << a, and calls the corresponding operator<<() function (which takes *one*argument, so there is no question about the order of evaluation of function arguments). The function then returns the ostream value that invoked it (in this case, cout) so now the expression looks like this:

    Code:
    cout << b;
    This once again evaluates to the right operator<<() call.

    And yes, the operator<<() call that processes "a" is called first, and the operator<<() call that processes "b" is called second and this is perfectly well defined and that's why "a" gets inserted into the output stream before "b" does.

    The only reason

    Code:
    cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;
    is undefined is because you are trying to modify the *same* variable three times. If it were 3 different variables, as such.

    Code:
    int var1, var2, var3;
    ... // initialize them to something
    cout<<var1<<" "<<var1++<<" "<<++var2<<" "<<var3++;
    then everything, including the output and the order in which the functions are called, is well defined.

    Note that

    Code:
    cout << var1 << " " << var1++;
    is OK because even though you're outputting the same variable twice, you're only actually modifying it once.
    Old Unix programmers never die, they just mv to /dev/null

  15. #15
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: What is the Output of this program?

    Quote Originally Posted by HighCommander4
    is not one function call. First, the compiler looks at

    cout << a, and calls the corresponding operator<<() function (which takes *one*argument, so there is no question about the order of evaluation of function arguments).
    Nitpick: it takes two arguments - don't forget the ostream&. Otherwise, you're quite right.
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured