Assignment or increment operator in c++ [duplicate] - c++

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
What's the reason for letting the semantics of a=a++ be undefined?
(8 answers)
Closed 5 years ago.
I have been running this code at different compilers. At Microsft VS. it prints 1, but at gcc, it prints 0. What is the result according to the standard c++. I don't if there is standardization for this piece of code as well.
int a=0;
a=a++;
cout << a<< endl;

a=a++ is undefined behavior. Not only is there no standard definition for what will happen, it isn't even guaranteed to always do the same thing between different runs.
It could print 0 now, 1 the next time, and crash your program on the third attempt.

Related

Same code gives different output on Microsoft studio compared to other compilers [duplicate]

This question already has answers here:
How do we explain the result of the expression (++x)+(++x)+(++x)? [duplicate]
(5 answers)
Closed 3 years ago.
int a = 10;
a = a++;
cout << a<<" ";
a++;
cout << a;
In microsoft Visual its output is 11 12
And in other compilers (like codechef and codeforces) its output is 10 11
This question's Output was asked in some mcq paper and the correct answer is 10 11 but can any1 also explain how
a = a++;
++a;
cout<<a;
produces output of 11 does a = a++ does't do anything?
a = a++; is not a correct statement due to lack of sequence point or being unsequenced(C++11).
undefined behavior - there are no restrictions on the behavior of the program. Examples of undefined behavior are memory accesses outside of array bounds, signed integer overflow, null pointer dereference, more than one modifications of the same scalar in an expression without any intermediate sequence point (until C++11)that are unsequenced (since C++11), access to an object through a pointer of a different type, etc. Compilers are not required to diagnose undefined behavior (although many simple situations are diagnosed), and the compiled program is not required to do anything meaningful.
https://en.cppreference.com/w/cpp/language/ub

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!)

Post-increment within a self-assignment, different results between VS2013 and GCC [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 7 years ago.
The codes in this Post-increment within a self-assignment, testing with C++.
However I get different result between VS2013 and GCC4.8.4
int cc = 42;
cc = cc++;
cout << cc << endl;
The result of VS2013 is 43, but the result of GCC is 42. It confuse me...
Which result is correct?
This is textbook undefined behavior. The compiler can do whatever the hell it wants to. By the language spec, it is allowed to halt and catch fire, should it so choose; be glad they just did something vaguely related to the nonsense you fed them.

C++ Operator Precedence: GCC 5.2 vs Visual Studio 2015 [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
Lets take simple C++ code like:
int main(){
int a = 0;
while(a<3) {
a=a++;
std::cout<<a<<std::endl;
}
}
This code built using Visual Studio 2015 print 1, 2, 3 when g++ 5.2.0 goes into an infinite loop and print only zeros.
According to C++ Operator Precedence assignment operator (=) has a lower priority then a post-incrementation. It would suggest that first zero is assignment to variable 'a', after that 'a' is incremented, so after first iteration a = 1. So result obtained from VS 2015 is right. Why GCC produce different output?
Your pogram is invalid (Undefined behavior) thus the compiler can generate anything.
The problem is your assigning to a single variable more than once in a statement (something to do with sequence points).
a=a++;
Thus should be:
a++;

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