|
-
June 22nd, 2005, 01:50 PM
#1
What is the Output of this program?
void main()
{
int var1 = 10;
cout<<var1<<" "<<var1++<<" "<<++var1<<" "<<var1++;
}
And why? Thanks for your inputs.
-
June 22nd, 2005, 01:52 PM
#2
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
-
June 22nd, 2005, 01:54 PM
#3
Re: What is the Output of this program?
 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:
- it's int main(), not void main()
- 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
-
June 22nd, 2005, 01:57 PM
#4
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.
-
June 22nd, 2005, 02:00 PM
#5
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.
 Originally Posted by Graham
Maybe I should expand on that:
- it's int main(), not void main()
- 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.
-
June 22nd, 2005, 02:03 PM
#6
Re: What is the Output of this program?
 Originally Posted by Bond
because you forgot to include <iostream.h>
Scratch the .h and we agree.
 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
-
June 22nd, 2005, 02:10 PM
#7
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.
 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.
-
June 22nd, 2005, 02:11 PM
#8
Re: What is the Output of this program?
 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
-
June 22nd, 2005, 02:15 PM
#9
Re: What is the Output of this program?
Even you are right, there is always logics behind the output, which I try to understand.
 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
-
June 22nd, 2005, 02:35 PM
#10
Re: What is the Output of this program?
 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.
-
June 22nd, 2005, 03:20 PM
#11
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
-
June 22nd, 2005, 03:23 PM
#12
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
-
June 22nd, 2005, 03:29 PM
#13
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
-
June 22nd, 2005, 04:39 PM
#14
Re: What is the Output of this program?
 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
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:
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
-
June 22nd, 2005, 04:56 PM
#15
Re: What is the Output of this program?
 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|