Post Increment in C++ [duplicate] - c++

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I know the concept of post increment but how does it apply to the follow? The output of t is 10. How to explain the undefined behavior?
int a = 2;
int b = 3;
int t;
t = a++ * (a+b);

That is undefined behavior you have there.

Related

looks like there is something wrong with the pre-increment operator in c++ [duplicate]

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 19 days ago.
I was just messing with a piece of cpp code that I came across something I couldn't find an explanation for.
when I run this in c++:
int a = 5;
cout << (--a) * (--a);
9 is printed in the console. but when you get the variable (a) from the input, and then give it the same value (5), the result is different:
int a;
cin >> a;
cout << (--a) * (--a);
when you enter 5 as the input, 12 is printed in the console. Why is it so?

pre-increment additions confusion [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Which of the following combinations of post & pre-increment operators have undefined behaviour in C?
(7 answers)
Closed 6 years ago.
Hello Stackoverflowers,
Considering the following pre-increment addition, can you explain me why j = 8 in the following code ?
int i = 2;
int j = ++i + ++i;
//j = 8, why ??

Unexpected C++ glitch - incrementation behavior [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 8 years ago.
When i run this code the var "tarkiz" remains 1.
Could anyone please explains to me why this happens? isn't it supposed to perform the assignment first (tarkiz = tarkiz) and then increment the value to be 2 instead of 1?
#include <iostream>
using namespace std;
int main() {
// your code goes here
int tarkiz = 1;
tarkiz = tarkiz++;
cout<<tarkiz<<endl;
return 0;
}
tarkiz = tarkiz++; is undefined behavior. To fix it write tarkiz++; instead.
See this explanation for details.

Why does this program output 4, instead of 3? [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Output of multiple post and pre increments in one statement [duplicate]
(1 answer)
Closed 8 years ago.
int main()
{
int var = 1;
var += ++var;
cout<<var;
return 0;
}
In Java this will output 3, as expected, but in C++ (above) it outputs 4. Why?
Because this is undefined behaviour. You're modifying and accessing the same variable without an intervening sequence point, so the outcome is really up to the compiler. If you compile this with clang, you'll see:
unsequenced modification and access to 'var'
I actually get 4 as the answer, but it could equally be 3, 7, 123125123 or "Lobster".

what will be behavior of following code snippet? [duplicate]

This question already has answers here:
float to int unexpected behaviour
(6 answers)
Closed 6 years ago.
What will be the behavior and output of the following code if i accidentally code so in C/C++,
float a = 12.5;
printf("%d\n", a);
printf("%d\n", *(int *)&a);
Rubish and more rubish.
You would get something meaningful if you did the following though
printf("%d\n", (int)a);
its undefined, because ANSI defines not a relation between sizeof(int) and sizeof(float).