This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
How is the postfix and prefix increment operator evaluated in an expression? [duplicate]
(1 answer)
Closed 5 years ago.
I thought I understand how postfix and prefix works until I encounter this
#include <iostream>
using namespace std;
int main(int argc, char const *argv[]) {
int a = 3, b = 3, c = 3;
cout << a + a++ << endl; // = 7 Okay, makes sense to me
cout << b + c++ << endl; // = 6 Now I'm confused
int x = 3, y = 3;
cout << (x++)*(x++)*(x++) << endl; // = 3*4*5 = 60 Okay I get it
cout << (++y)*(++y)*(++y) << endl; // = 5*5*6 = 150 Wait what ? But why... ?
// I thought it would be 4*5*6 or 6*6*6 or something
return 0;
}
Is there any logical explaination for this ? Or it's some kind of undefined behaviour ?
C++ does not provide guarantees wich argument will be produced first.
When you code something like:
a + a++
it will be translated to
int operator+(int a1, int a2)
before a call, this function a2 and a1 must be calculated. You can't determinate which parameter will be performed fist. So, you have undefined behavior.
Related
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 5 years ago.
I am using the increment operator in prefix n postfix in C++, but i m getting incorrect results, can anyone please guide me n tell me what i m doing wrong :) Thank you :)
here is my code :)
#include <iostream>
using namespace std;
int main()
{
int a=0;
cout<<a++<<endl<<++a<<endl;
}
Expecting Result
0
2
but i m getting Result
1
2
This has to do with the increment operator++ and how it functions, depending if you use it as prefix or a postfix:
#include <iostream>
int main() {
int a = 0;
std::cout << a++ << std::endl
// Output: 0. ++ as posfix creates an incremented copy of "a" and AFTER the line gets executed.
std::cout << ++a << std::endl;
// Output: 1. ++ as prefix creates a copy of "a", an incremented copy BEFORE the line gets executed.
std::cout << a++ << std::endl << ++a << std::endl;
// Output: 0
// 1.
// "a" get incremented only by the prefix operator++
}
Note: changed using namespace std and I recommend you use namespace+scope operator, like std:: for C++ Standard Library containers.
This question already has answers here:
Unexpected order of evaluation (compiler bug?) [duplicate]
(3 answers)
Undefined behavior and sequence points
(5 answers)
Closed 6 years ago.
I came across this piece of code:
#include <iostream>
int main()
{
int m = 44;
std::cout << "m = " << m << ", m++ = " << m++ << ", ++m = " << ++m <<
std::endl;
return 0;
}
My first thought was, this is undefined behavior, but it turns out this code was part of an exam in my university, so I'm not so sure now.
The question on the exam is: what is the output of this program?
I also heard that C++11 or C++14 changed the rules about UB in some way, so it might be defined now.
Anyway, the output is (on gcc in windows)
m = 46, m++ = 45, ++m = 46
Is that output correct?
This question already has answers here:
Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++14?
(1 answer)
Does initialization entail lvalue-to-rvalue conversion? Is `int x = x;` UB?
(4 answers)
Closed 6 years ago.
I am confused about the following code:
#include <iostream>
int i = 1;
int main()
{
int i = i;
std::cout << "i: " << i << "\n";
return 0;
}
Output:
i: 0
I had expected running the above code would print 1. Can someone please explain the reason for this strange behavior?
You are initializing i with itself. The both i's in int i = i; are the inner one not the outer one. This is undefined behavior and you may get 0 or anything may happen.
This is the right way if you want to assign the outer i to the inner i.
#include <iostream>
int i = 1;
int main()
{
int i = ::i;
std::cout << "i: " << i << "\n";
return 0;
}
Live Demo
BTW, You should carefully read all the compiler warnings. If you did you could see the problem yourself:
warning 'i' is used uninitialized in this function
This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
Why the output of next code is 2 1 2?
#include "iostream"
int main(int argc, const char *argv[])
{
int i = 0;
std::cout << i << std::endl << i++ << std::endl << ++i << std::endl;
return 0;
}
Because first i is equal 2 but not zero, it means that the whole like of cout is evaluated first
and then printed (not part by part). If so, then first value should be 1, but not 2, because i++ should increment i after printing. Could you clarify?
EDIT:
The output of next code is 2 2 0.
#include "iostream"
int main(int argc, const char *argv[])
{
int i = 0;
std::cout << i << std::endl << ++i << std::endl << i++ << std::endl;
return 0;
}
why?
There is no sense reasoning in the output of your code because as it stands your program exhibits Undefined Behavior.
Per paragraph 1.9/15 of the C++11 Standard:
The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined.
Because there is no sequence point separating both mutations of i, Undefined Behavior ensues. Your compiler might not output anything, and the program might output differently on different compilers. But arguing about the output is unnecessary in this context.
If you separate the statements, the result will then come out as expected:
std::cout << i << std::endl; // 0
std::cout << i++ << std::endl; // 0
std::cout << ++i << std::endl; // 2
the evalution goes from right to left.
i = 0
++i -> i = 1
i++ -> i = 1, post incrementation, a copy occurs. then i = 2
i -> i = 2
As all this occurs before being send to cout, the value of i is 2, and the middle one have been copied and its value is 1.
Please tell me if I don't understand your question clearly:
cout << i++;
is the equivalent of
cout << i;
i+=1;
while cout << ++i
is the equivalent of
i += 1;
cout << i;
in otherwords any time you use i++, post-increment it returns the current value then changes while ++i means increment first then return the new value. It has nothing to do with cout
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Undefined Behavior and Sequence Points
The variable i is changed twice, but is the next example going to cause an undefined behaviour?
#include <iostream>
int main()
{
int i = 5;
std::cout << "before i=" << i << std::endl;
++ i %= 4;
std::cout << "after i=" << i << std::endl;
}
The output I get is :
before i=5
after i=2
Yes, it's undefined. There is no sequence point on assignment, % or ++ And you cannot change a variable more than once within a sequence point.
A compiler could evaluate this as:
++i;
i = i % 4;
or
i = i % 4;
++i;
(or something else)