Operator ++ with stdout giving unexpected result in C/C++ [duplicate] - c++

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
During study in C++ at school, when we learn about operator ++ in C++, we know ++c and c++ are different.
When we test more about this with this kind of code:
#include <iostream>
using namespace std;
int main(){
int c=10;
cout<<++c<<" "<<c++<<endl;
return 0;
}
Why did above code gave output of 12 and 10 in C++?
The computer (we tests with both cout and printf, we also tried VC++ and g++) give this to me:
12 10
Both "cout" and "printf" gave the same result.
But when we test in calculation, the result is all right.
#include <iostream>
using namespace std;
int main(){
int c=10;
int r=++c^c++;
cout<<r<<endl;
return 0;
}
Above source code gave me 0 which means while above XOR operation executing, both left hand side (++c) and right hand side (c++) giving the same value to XOR operator which is 11 (We get the value 11 by replacing c++ with 11 and computer gives the same result 0).
This is really wired. Did anyone noticed this?
By the way, we test in both debug mode and release mode in both Windows and Lubuntu. So we think this is relating to the standard library. But we aren't expecting that we can read the stdlib as a NOOB. So hoping someone can find a reason or solution.

The code int r=++c^c++; is undefined behaviour and should not be used, not ever. You can't modify the variable twice before sequence point.

Related

Cout works well on wrong-written function [duplicate]

This question already has answers here:
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Closed 5 months ago.
I try to help people learn to code in C++ and I received an answer for an easy task to write a code that gets three numbers as input and outputs a sum of them. I received a wrong-written code with no return statement which... suprisingly works. It prints a good answer to the console, which (as far as know) shouldn't happen without return. I know it is written wrong and I will reply how it should be done but I want to be precise and include an answer why it worked. Here is the code:
#include <iostream>
using namespace std;
int add ()
{
int a,b,c, result;
cin >> a;
cin >> b;
cin >> c;
result=a+b+c;
}
int main()
{
cout << add();
return 0;
}
I would be grateful for an answer.
In some smart compilers it can run the code. but warning will be still there that function is expected to return integer but it is not returning anything.
but in environment like visual studio it will give the error and program will not be build.

Output is not what I want C++ [duplicate]

This question already has answers here:
How to output float to cout without scientific notation or trailing zeros?
(3 answers)
Closed 3 years ago.
I got some problems . I input a=5.999 with d=1 and expected output is 6 but its output is 6.0. I mean I want to delete .0 in my output if it had.
Example: Input:
a=3.864361,d=3,Output expected: 3.864/ Input:a=5.9577,d=1 Output expected: 6/ Input:
a=0.07266,d=3,Output expected:0.073.
#include <iomanip>
using namespace std;
int main()
{
float a;
int d;
cin>>a>>d;
cout<<setprecision(d)<<fixed<<a;
return 0;
}
Sadly, this is one example I know of where C++ solution is purely inferior to C. To the best of my knowledge, there is no way to do that with C++ streams.
On the other hand, C has printf format specifier exactly for that: %g
Following code will do what you want (don't forget to include stdio.h):
printf("%.1g\n", a);

Getting Different ouputs on Diffrent compilers [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I got stuck in operator precedence problem...In the first look it looks quite easy but it is really hard ...and i run it on DEV as well as Visual studio but it gives different output and i m completely shocked .here is my code
#include<iostream>
using namespace std;
int main(){
int a=0;
a=++a*++a*a++;
cout<<a<<endl;
}
it gives 8 output in Dev and g++ but 9 in Microsoft Visual studio 2013, any help would be appreciated ...Also plz tell me in which order does these operators call each other so that to get the desired output on the console.
a=++a*++a*a++;
Should be
++a;++a; //a = 2, because you have the two pre-increments.
temp = a * a; //temp = 4
temp = temp * a; //temp = 8
a = temp; //a = 8
a++; //a = 9, for the post-increment.
So you should probably report this to the compiler teams along with your CPU specs.
However, according to gnu.org regarding operator precedence:
In C you cannot assume that multiple subexpressions are evaluated in
the order that seems natural. For instance, consider the expression
++a * f(). Does this increment a before or after calling the function f? The compiler could do it in either order, so you cannot make
assumptions.
(Note that the above is taken from the GNU c manual).
So technically this is not a bug, even though it's inconsistent.
Short term solution:
a = 9;
f you really wrote code like this, you are in trouble.
Better you must to use aux variables like
#include<iostream>
using namespace std;
int main(){
int a=0;
int aux = ++a;
a=aux*aux*a++;
cout<<a<<endl;
}
or something like this to solve the ambiguity.
If you question is for learning purposes, there are cases that has no an unique result, because is the way that the compiler solve this equation.
Dr.dobbs's Magazine show several cases like this, and all equations haven't an exact result.
I can't get now some page to show code you never will use, to avoid bugs like this.
Just for example: http://www.cplusplus.com/forum/beginner/26383/
This is a sequence-point issue, and should have warned you about it if you have -Wsequence-point enabled. In essence your statement is undefined, and is why different compilers give different results.
Please look at a previous answer to this problem: undefined-behavior-and-sequence-points

How does C++ process this [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 7 years ago.
I have here an equation i can't understand how c++ process this. Can someone explain this operation?
code:
#include <stdio.h>
main(){
int a[10] = {0,1,2,3,4,5,6,7,8,9};
int i = 0;
int num = a[i+++a[++i]]+a[++i+i++];
printf("\nnum1: %d i: %d,num,i);
}
why is the answer num = 9 while index i is just equal to 4;
Using ++ twice in the same expression on the same variable is explicitly undefined by all versions of both the C and C++ standards, and so i does not necessarily equal 4. It could be anything at the whim of the compiler writer.
Never do this. Never use ++ and -- twice in the same expression. There is no way to make any statement about what the resultant value will be, and no experience with what it does with one compiler will mean anything with respect to what another compiler does.

Obscure C++ syntax [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ Comma Operator
About a year ago I noted some obscure syntax in a coding project I was working on:
table_value = table_index += 2, valueFromTable(table_index);
Does anyone recognise this?, it's like an assignment with an additional statement. This compiled in our entire suite of cross-platform compilers, so I'm pretty certain its valid C++ but I've never seen anything like it.
Any insight would be appreciated.
Gearoid
EDIT: heres some working code:
#include <iostream>
using namespace std ;
int valueFromTable(int a) { return a ; }
int main()
{
int table_index = 0 ;
int table_value = table_index += 2, valueFromTable(12);
cout<<table_value<<endl;
return 0 ;
}
This is the Comma operator.
It's standard C and C++ but heavily frowned upon.
It evaluates both arguments, and returns the result of the second.