Hello, I am new to this forum and I was wondering if I could get some assistance!
I've come across a problem where the code I'm working with outputted a value that I didn't expect. When I ran the code below, the expected result was "5 4 4 3 2 1". However, the output is "5 4 3 4 2 1." I'm confused as to why the output value seems to have increased from 3 to 4. I believe this is because on line 10 it outputs 2 'a' values. When I moved the second a value to a second line and made it its own statement, the output was exactly how I originally anticipated it. Is this because of how the compiler reads the statement? An explanation would be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << a-- << ' ';
cout << a << ' ';
cout << a-- << ' ' << a-- << ' ';
cout << a-- << ' ' << a << endl;
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << a-- << endl; // a = 5, print a (5), a-- -> a = 4
cout << a << endl; // a = 4, print a (4), a -> a = 4
cout << a-- << endl; // a = 4, print a (4), a-- -> a = 3
cout << a-- << endl; // a = 3, print a (3), a-- -> a = 2
cout << a-- << endl; // a = 2, print a (2), a-- -> a = 1
cout << a << endl; // a = 1, print a (1), a -> a = 1
return 0;
}
Thanks kemort but the second source code is after I modified it to figure out that having the output on the same line (line 10) made a difference in the output. The first source code I posted is the one I'm having issues with. Thank you though
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << a-- << ' ';// print 5, a = 4
cout << a << ' ';// print 4, a = 4
cout << a-- << ' ';// print 4, a = 3;
cout << a-- << ' ';// print 3, a = 2
cout << a-- << ' ' << a << endl; // print 2, a = 1, print 1
return 0;
}
I see your point!!
This is what is supposed to happen as you probably know and hence your question. "Postincrementing (or postdecrementing) causes the current value of the variable to be used in the expression in which it appears, then the variable’s value is incremented (decremented) by 1."
Actually I'm just going to delete the second bit of code since I think its causing some confusion. Also an explanation as to why the output value goes from 4 to 3 then back to 4 would be appreciated. I'd like to understand why this is happening rather than just knowing the output.
This is the code I'm having issues with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << a-- << ' ';
cout << a << ' ';
cout << a-- << ' ' << a-- << ' ';
cout << a-- << ' ' << a << endl;
return 0;
}
If a side effect on a scalar object is unsequenced relative to another side effect on the same scalar object, the behavior is undefined.
> cout << a-- << ' ' << a << endl;
If a side effect on a scalar object is unsequenced relative to a value computation using the value of the same scalar object, the behavior is undefined.
Tip: compile with warnings enabled: -Wall -Wextra
Demand strict standard conformance (to the extent that the implementation is capable of): -std=c++14 -pedantic-errors
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <iostream>
usingnamespace std;
int main()
{
int a = 5;
cout << a-- << ' ';
cout << a << ' ';
cout << a-- << ' ' << a-- << ' ';
cout << a-- << ' ' << a << endl;
return 0;
}
echo && echo clang++ && clang++ -std=c++14 -stdlib=libc++ -Wall -Wextra -pedantic-errors -O3 -c main.cpp
echo && echo g++ && g++ -std=c++14 -Wall -Wextra -pedantic-errors -O3 -c main.cpp
clang++
main.cpp:10:13: warning: multiple unsequenced modifications to 'a' [-Wunsequenced]
cout << a-- << ' ' << a-- << ' ';
^ ~~
main.cpp:11:13: warning: unsequenced modification and access to 'a' [-Wunsequenced]
cout << a-- << ' ' << a << endl;
^ ~
2 warnings generated.
g++
main.cpp: In function 'int main()':
main.cpp:10:36: warning: operation on 'a' may be undefined [-Wsequence-point]
cout << a-- << ' ' << a-- << ' ';
^
main.cpp:11:35: warning: operation on 'a' may be undefined [-Wsequence-point]
cout << a-- << ' ' << a << endl;
^