This question already has answers here:
ostream chaining, output order
(5 answers)
Closed 8 years ago.
below is my practice code about c++11 lambda:
#include<iostream>
int d = 0;
int main()
{
int e = 1;
auto i = [&]() ->int {
e += 1;
d += 1;
return d;};
d += 1;
std::cout << "the value of d:" << d << std::endl;
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << std::endl;
std::cout << " e:" << e << " d:" << d << std::endl;
return 0;
}
and i got result not as expected:
the value of d:1
the value of i():2
e:1 d:1
e:2 d:2
I just don't understand why
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << std::endl;
std::cout << " e:" << e << " d:" << d << std::endl;
this two lines give different out put of e and d?
ps:forgive my bad english
The evaluation order of the operands of << isn't specified; so it's unspecified whether the values of e and d in the first line are taken before or after they're incremented by the call to i().
So you might get the values before or after they're incremented, depending on the whim of the compiler.
The second line is sequenced after the first, so you'll definitely get the incremented values there.
The order of evaluation of function arguments is unspecified. So function arguments can be evaluated right to left or left to right.
It seems that your compiler evaluates arguments from right to left. So in this statement
std::cout << "the value of i():" << i() << std::endl << " e:" << e << " d:" << d << << std::endl;
the compiler at first evaluates d that is equal to 1 then e that is equal also to 1 and only after that it evaluates i() that returns 2. So you get the following output
the value of i():2
e:1 d:1
Take into account that in this statement using of operator << is equivalent to a call of an appropriate overloaded operator function with the corresponding arguments.
For example this code snippet
int a = 10, b = 20;
std::cout << a << ' ' << b;
is equivalent to
int a = 10, b = 20;
std::operator <<( std::cout.operator <<( a ), ' ' ).operator <<( b );
To get the expected result you should split the original statement into
std::cout << "the value of i():" << i() << std::endl
std::cout << " e:" << e << " d:" << d << << std::endl;
Related
Hello I need help with this tracing problem, here here is a copy of the problem:
What is the output of the following program segment?
int u = 4, v = 3;
one(u, v);
cout << u << " " << v << endl;
cout << two(u, v) << " " ;
cout << u << " " << v << endl;
void one(int &x, int& y){
int a;
a = ++x ;
x += y++;
y = ++a;
}
int two(int s, int &t){
int b;
b = s – t;
s += t + b ;
t += 4 * b;
cout << s << " " << t << " " << b << endl;
return ( b ) ;
}
I managed to find the first output of function One, then I plugged it into function Two to find its output. But function Two returned the cout instead of the return (b), can anyone show me what I am doing wrong, any help would be greatly appreciated! Here is a copy of the output after plugging it into visual studios:
Output:
8 6
16 14 2
2 8 14
The relevant fragment is equivalent to the following, with the step-by-step breakdown in comments.
cout << u << " " // prints 8
<< v // prints 6
<< endl; // ends first line "8 6"
int n = two(u, v); // calls 'two' which prints the second line "16 4 2" and returns 2
cout << n // prints 2 which is the value returned by 'two' at the previous step
<< " " ; // prints a space but does not end the line
cout << u << " " // prints 8 next on the third line
<< v // prints 14
<< endl; // ends third line and prints "2 8 14"
I was playing around with pointers and got results I did not expect:
#include <iostream>
#include <vector>
int main() {
int arr[4] = { 1, 2, 3, 4 };
int* pArr = arr;
std::cout << "First number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nSecond number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nThird number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nFourth number: " << *pArr << " at address: " << pArr;
int* pArr2 = arr;
std::cout << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n";
/*
int* pArr2 = arr;
std::cout << "\n"
<< ++ * pArr2 << "\n"
<< * ++pArr2 << "\n";
*/
}
The two different results:
1 2 3 4 - as expected using the first method
4 3 2 1 - using cout with multiple arguments I do not know the proper name.
So my question is - why does this happen? Using multiple cout statements results in expected for me code, while using just 1 cout results in backwards solution.
As a side note, another confusing thing to me is that pre-increment results in all values being equal. In the commented bit of code, the result is 3 3, no matter the ++ placement with respect to the *.
This code:
std::cout << "\n" << *pArr2++ << "\n";
std::cout << "\n" << *pArr2++ << "\n";
has a well defined order of modifications to pArr and will print
1
2
But this code:
std::cout << "\n" << *pArr2++ << "\n" << *pArr2++ << "\n";
invokes undefined behavior before c++17, because there are multiple modifications to pArr2 that are unsequenced. The program has UB, so it could print anything.
From c++17, there is a sequence point between the modifications, and the above code is guaranteed to print:
1
2
This question already has answers here:
Order of evaluation of arguments using std::cout
(5 answers)
Strange output, not as expected
(2 answers)
Undefined behavior and sequence points
(5 answers)
Closed 5 years ago.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int a = 5;
int& b = a;
int* c = &a;
cout << "CASE 1" << endl;
cout << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl << endl;
cout << "CASE 2";
a = 5;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << ++b << endl << "c is " << *c << endl << endl;
cout << "CASE 3";
a = 5;
cout << endl << "a is " << a << endl << "b is " << b << endl << "c is " << *c << endl;
b = 10;
cout << endl << "a is " << a << endl << "b is " << b++ << endl << "c is " << *c << endl;
}
The output:
CASE 1:
a is 5. b is 5. c is 5.
a is 10. b is 10. c is 10.
CASE 2:
a is 5. b is 5. c is 5.
a is 11. b is 11. c is 10.
CASE 3:
a is 5. b is 5. c is 5.
a is 11. b is 10. c is 10.
I understand CASE 1. But I am having difficulty understanding CASE 2 and CASE 3. Can someone explain why doesn't c get updated with new value in both cases?
The evaluation order of operands is unspecified, and you're modifying an object and reading it without those operations being sequenced.
Thus, your program is as undefined as cout << a << a++;, and anything can happen.
I think your problem is due to what is called sequence points. You can have a long read about that in this answer but in a few words it basically states the order or evaluation of the elements of an expression.
Update in your case this order is undefined, although some compilers seem to make it right-to-left.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm using C++, and I have increments and decrements down solidly, but I have one equation where I have to decrement theChar equation, or the one with int var by -2, and I do not know the code for it.
Please formulate your question better. What do you mean with
I have one equation where I have to decrement "theChar" equation, or
the one with "int var" by -2
Do you mean:
char x = 'a';
x = x + 3; //now x is 'd'
int var = 10;
var -= 2; //equal to var = var -2;
The equations are not equations in a math sense of equation.
The = sign tells the computer to store whats on the right side into the variable that is on the left side.
int a;
a = 5;
this stored the 5 into a once.
int a, b;
a = 5;
b = a;
a = 6;
b is still 5 because when it was stored it copied from a. When a changed b was not recalculated but stayed as it was.
int a;
a = 5;
a = a - 2;
a is now beeing decremented by 2 since a was set to 5 when the right side ( a - 2 ) is calculated it is calculated to be 3. When that is done it gets written to the left side which happens to be a so at this point a is overwritten with 3;
char c = 'B';
c = c - 1;
c has a value of 'A' at the end of this code. There is some behind the scenes magic going on.
Characters are also numbers. So what really happens when i store 'B' into the variable the computer actually stores 66. You can read up on that here. When i decrement by one the value is decremented from 66 to 65. The character with the number 65 happens to be 'A'.
I read in the comments that you have trouble putting it all into a program.
I went ahead and wrote a code snippet for you.
#include <iostream>
using namespace std;
int main()
{
int a, b;
char c;
//cout is like a friend that you give something to put on the console
// << means give this to cout
cout << "Hello World!" << endl; //endl is a new line character
cout << endl << "Setting a, b" << endl;
a = 5;
b = a;
cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl;
cout << endl << "Changing a" << endl;
a = 3;
cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl;
cout << endl << "Adding to a" << endl;
a = a + 3;
cout << "Value of a is " << a << ". " << "Value of b is " << b << "." << endl;
cout << endl << "Playing around with characters" << endl;
c = 'B';
cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl;
c = c + 1;
cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl;
c = 70;
cout << "Character c is " << c << ". " << "Number stored for c is actually " << (int)c << "." << endl;
}
// H2.cpp : Tihs program runs different mathmatical operations on numbers given by the user
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a;
cout << "Enter a number for a: "; //Prompts the user for a and b inputs
cin >> a;
int b;
cout << "Enter a number for b: ";
cin >> b;
cout << "A is " << a << "\tB is " << b << endl;
cout <<"Sum of a and b is equal to " << a << " + " << b << " and the result is " << (a + b) << endl; //Performs addition operator and gives output
cout <<"Product of a and b is equal to " << a << " * " << b << " and the result is " << (a * b) << endl;
cout <<"a > b is " << a << " > " << b << " and the result is " << (a > b) << endl;
cout <<"a < b is " << a << " > " << b << " and the result is " << (a < b) << endl;
cout <<"a == b is " << a << " == " << b << " and the result is " << (a == b) << endl; //Performs boolean operator and outputs result
cout <<"a >= b is " << a << " >= " << b << " and the result is " << (a >= b) << endl;
cout <<"a <= b is " << a << " <= " << b << " and the result is " << (a <= b) << endl;
cout <<"a != b is " << a << " != " << b << " and the result is " << (a != b) << endl;
cout <<"a -= b is " << a << " -= " << b << " and the result is a = " << (a -= b) << endl; //Performs - operator on a - b and makes a equal to the new result
cout <<"a /= b is " << a << " /= " << b << " and the result is a = " << (a /= b) << endl;
cout <<"a %= b is " << a << " %= " << b << " and the result is a = " << (a %= b) << endl; //Performs % operator on a % b and makes a equal to the new result. Ripple effect created from previous 2 lines as the value of a changes each time.
return 0;
The output I'm concerned with is here:
a -= b is -4198672 -= 4198672 and the result is a = -4198672
a /= b is -1 /= 4198672 and the result is a = -1
a %= b is -1 %= 4198672 and the result is a = -1
It seems like the value for a being displayed is the value of a after the line of code is executed. I'm sure that has something to do with the order of operations, but I'm not sure how to get around that. Any help is greatly appreciated.
The order in which arguments to operators or functions are evaluated is undefined in C++. If the evaluation of the different arguments have side effects, these can happen whenever the compiler sees fit and if there multiple modifications to the same object the result is undefined behavior. If you want to force a specific evaluation order, you'd need to break your expression down into multiple separate expression as these are evaluated in order or you could inject operators which create a sequence point. However, the operators creating a sequence point don't play nicely with chaining output operators. The list of operators forcing the first argument to be evaluate before the other arguments are:
the , operator
the logical && and || operators
the conditional operator ?:
Of course, if you overload any of these operators, they stop introducing a sequence point as they become normal function calls.
All the operators you have listed are comparison operators which return value true or false.
Except: -=, /= and %= operators
a-=b is actually a=a-b.
Just check if you are sure that you wanted this in first place?
BTW: if(a=b) is also returns true since it is always success but I do not think we do this intentionally.