Increment order [duplicate] - c++

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Is the behaviour of i = i++ really undefined?
(9 answers)
Closed 3 years ago.
I am facing problems in understanding the increment orders in C++.
I am aware that increments are unary operators thus they come after parenthesis from right to left.
My question is when do we increase the number?
Here is a simple code:
#include <iostream>
using namespace std;
int main()
{
int a1;
int a(12),b(3);
a1=7+10%3-5;
b=a/b++;
cout<<a1<<"\t"<<b<<endl;
return 0;
}
Here I get a=3 that's right but b=5 , I think it is 3 because we start from the right and increase by 1 then 12/4 gives 3.

Note that the C++ grammar implies that the associativity of the postfix increment is from left to right, and that of the prefix increment is right to left.
The behaviour of b = a / b++; is actually undefined. This is because = is not a sequencing point, so there are simultaneous reads and writes on b.
(The same applies to C.)
It's a variant on i = i++;: for more on that see Is the behaviour of i = i++ really undefined?

Related

Why Overflow is not in circular fashion [duplicate]

This question already has answers here:
bool operator ++ and --
(4 answers)
Closed 2 years ago.
why incase of boolean, overflow doesn't occur in circular fashion. eg say a=126 when you reach 128 and you increment it, a goes to -127 if range is -127 to 128. similarly for boolean it is 0 to 1 so it should move around 0101010101 and so on. please clarify
using namespace std;
int main()
{
bool a;
for (a = 1; a <= 5; a++)
cout << a;
return 0;
}
From cppreference
If the operand of the pre-increment operator is of type bool, it is set to true
If the operand of the post-increment operator is of type bool, it is set to true
Note that this behaviour was removed in C++17 and your code won't compile with newer standards (probably because it was confusing).

unexpected output of the following program [duplicate]

This question already has answers here:
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Closed 21 days ago.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int y,**p,*pt;
int x=5;
pt=&x;
p=&pt;
y=**p + ++(**p);
cout<<*pt<<" "<<**p<<" "<<x<<" "<<y;
getch();
return(0);
}
output generated 6 6 6 11, why not 6 6 6 12 kindly guide on execution step.
Here my doubt is **p is pointing to x only which is incremented by second operand ++(**p). so value of y should be 12.
This is classic undefined behaviour! There is no guarantee in the C++ standard about the order of evaluation of the operands of the + operator in y=**p + ++(**p).
I tested your code in MSVC and clang-cl and I get the output: 6 6 6 12 - which suggests (as you expect) that ++(**p) is evaluated first. However, on your compiler, it seems that the LHS is evaluated first.
From the cppreference site linked in the comments by Scheff:
Order of evaluation of any part of any expression, including order of
evaluation of function arguments is unspecified (with some exceptions
listed below). The compiler can evaluate operands and other
subexpressions in any order, and may choose another order when the
same expression is evaluated again. There is no concept of
left-to-right or right-to-left evaluation in C++….
PS: Interestingly, changing to y = ++(**p) + **p; also gives 6 6 6 12 as the output.

Undefined Behaviour in C++ [duplicate]

This question already has answers here:
Program behaving strangely on online IDEs
(4 answers)
Closed 3 years ago.
I got this from a facebook post. What's happening here? See the output in ideone. Output is more than 10 lines.
Code:
#include<iostream>
using namespace std;
int main()
{
for (int i = 0; i < 10; ++i)
cout << i*1000000000 << endl;
}
Ideone Link
Your platform most likely has a 32 bit int. So 1'000'000'000 is an int, and the compiler will attempt to evaluate i * 1'000'000'000 as an int too. This results in an overflow from i being 3 onwards.
The behaviour on overflowing a signed integral type is undefined.
Note that this makes the entire program behaviour undefined, which accounts for the multiple lines of output (beyond 10) that you observe.
(If you had chosen 10'000'000'000 say instead then the multiplication would have been evaluated with long long types and the behaviour would be well-defined!)

Macros preincrement in c++ [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
1.macros has always been difficult.
2.Below is the code and output is 125 and 7....Please elaborate the working
#define mul(x) (x++ * ++x * x++)
#include<iostream.h>
void main()
{
int a=4,j;
j=mul(a);
cout<<j<<endl;
cout<<a<<endl;
}
Your program results in undefined behaviour. j could be anything.
Read more here: Undefined behavior and sequence points
What I believe is happening is this.
Set a to 4.
mul is called and a is passed in.
The second multiplication (++x) has the prefix incrementer, so x (a) is increased by 1. a is now 5.
The multiplication now happens. Since the first and last have the postfix incrementer, nothing happens until after the multiplication happens. So we have 5 * 5 * 5 = 125.
Since the multiplication is done now, the postfix incrementers happen which makes x (a) 6 and then 7.
j then equals 125.
???
Profit!
EDIT:
This is explaining the behavior that he is experiencing. I understand that this won't happen in every case.

What is output of following code? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 9 years ago.
What will be the output of code
int a=3,b=4;
++a*=++b;
cout<<a<<endl;
compiler shows a=20 output.How precedence and operator associativity is being used here?
What I understand is:
first b on the left of ++a*=++b; is incremented us its unary operator then comes the
turn of *= so
expression becomes ++a = a * ++b; as a=3 and b=5 now so it becomes 15 then 15 is assigned to a and incremented.Finally getting 16 but compiler gives 20
In your particular case on your particular compiler, it seems that first a is incremented to 4 and b is incremented to 5, then a *= b executes and a becomes 20 (4*5). However other compiler could give different result because it is not a defined behaviour as people mentioned in comments