Stream extraction operator in C++ [duplicate] - c++

This question already has answers here:
unexpected output in cout and printf [duplicate]
(4 answers)
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I have the following code:
#include<iostream>
using namespace std;
int main()
{
int x=3;
cout<<x++<<++x<<x++<<++x<<endl;
return 0;
}
the output should be 3557
but it is 6747. why???
Also:
#include<iostream>
using namespace std;
int main()
{
int x=3;
cout <<x++<<endl;
cout<<++x<< endl;
cout<<x++<< endl;
cout<<++x<< endl;
return 0;
}
The above code gives:
3
5
5
7
(every digit in new line)
Can anyone explain why?

int x=3;
cout<<x++<<++x<<x++<<++x<<endl;
This is undefined behavior, so you could easily get any result.
int x=3;
cout <<x++<<endl; // x = 3, post incremented to 4...
cout<<++x<< endl; // here x = 4 and preincremented to x = 5
cout<<x++<< endl; // x = 5 , postincremented to 6...
cout<<++x<< endl; // so here x = 6 but preincremented to 7
Undefined behavior and sequence points
unexpected output in cout and printf

Related

How to covert int to double in c++ [duplicate]

This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 1 year ago.
I want to convert 3 to 3.00. I have done explicit type conversion. but guess it doesn't seem to work!whats wrong?
my code:
#include<iostream>
using namespace std;
int main(){
int PI = 3;
double a = static_cast<double>(PI);
cout<<a;
}
output: 3
This may help.
#include<iostream>
using namespace std;
int main(){
int PI = 3;
double a = static_cast<double>(PI);
cout.precision(2);
cout << std::fixed << a;
}

Condition Check in C++ [duplicate]

This question already has answers here:
Why do some experienced programmers write comparisons with the value before the variable? [duplicate]
(12 answers)
Closed 2 years ago.
Out of the two versions (below) of condition check which version is better? and Why?
Version 1:
#include<iostream>
using namespace std;
int main()
{
int x;
cin >> x;
if (x == 1) // version 1
{ cout << "Hello world!" << endl;
}
return 0 ;
}
Version 2:
#include<iostream>
using namespace std;
int main()
{
int x;
cin >> x;
if (1 == x) // version 2
{ cout << "Hello world!" << endl;
}
return 0 ;
}
These are the same, the only advantage I can see relates do error detection.
Lets say you mistakenly write 1 = x, you will have a compilation error.
If you write x = 1 then the condition will evaluate to true, x will be assigned the value 1, the program will compile fine but it will not have the expected ouptput and it might be hard to detect this kind of error, though you have compiler warnings that you can turn on for this kind of situation.

How can I print a a number upto 6 decimal digits in c++? [duplicate]

This question already has answers here:
Fixed Decimal Precision [duplicate]
(4 answers)
Closed 2 years ago.
I am trying a problem. I need to print the ans in 6 decimal digits. For example if the ans is 64, I want to print 64.000000
I tried the following way.
what I did wrong?
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
long long t;
cin>>t;
float n;
while(t--)
{
cin>>n;
float s=(n-2)*180.000000;
float w=(s/n)*1.000000;
cout<<w*1.000000<<setprecision(6)<<endl;
}
return 0;
}
You can make use of std::fixed:
#include <iostream>
#include <iomanip>
void MyPrint(int i)
{
double d = static_cast<double>(i);
std::cout << std::setprecision(6) << std::fixed << d << std::endl;
}
int main() {
MyPrint(64);
MyPrint(100);
return 0;
}
Running the above code online results in the following output:
64.000000
100.000000

Function pow doesn't show the right answer [duplicate]

This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 3 years ago.
I have this code and the function pow() is behaving differently in two cases.
For "p" I'm using the number 3, in the first output is showing 124 but the second shows 125.
#include <iostream>
#include <math.h>
using namespace std;
int main(){
long long int p, ans;
cin >> p; //3
ans = pow(5,p);
cout << ans << endl; //124
cout << pow(5,p) << endl; //125
return 0;
}

Why cout gives me a result in reverse order? [duplicate]

This question already has answers here:
Undefined behavior and sequence points
(5 answers)
Closed 8 years ago.
I'm newbie in c++ and i am doing some exercises.
I have a code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
int i=0;
int a,b,c;
a = i++;
b = i++;
c = i++;
cout << a << b << c;
return 0;
}
and, whe I run it, the result is: 012
But when I run it without variables a, b and c,
like in following code
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void) {
int i=0;
cout << i++ << i++ << i++;
return 0;
}
I get result in reverse order: 210
Why is this happening? I think it should be again 012 (I am using NetBeans)
You are violating the rules of the language that has multiple increment and decrement in the same expression or as different arguments to the same call as undefined.