#include <bits/stdc++.h>
using namespace std;
int f(int x)
{
int n;
if(x>0)
{
if(x%2==0)
{
cout<<x%10;
n=1+f(x/10);
}
else
{
n=1+f(x/10);
cout<<x%10;
}
return n;
}
else
return 0;
}
int main()
{
cout<<' '<<f(8174);
return 0;
}
So on my computer it displays " 48174" but on my friend's computer it displays "4817 4". The correct answer in the book is "4817 4". I don't understand why would the space be between numbers because I call cout<<' '; before I call the function. Can you help me understand which is the right way?
bits/stdc++.h
– Tolmachvoid f() { std::cout << "foo"; }
for the same effect with much simpler code – Yogurtauto x = f(8174); std::cout << ' ' << x;
orstd::cout << ' '; std::cout << f(8174);
– Yogurt' '
is guaranteed to be written first (in C++17). But the output of the return value off(8174)
and the side effect output in the recursive calls off
should be undetermined if I read correctly.. And of course I support writing code that does not rely on the exact rules.. – Tolmach<<
apply here: #38502087 and https://mcmap.net/q/732851/-c-17-evaluation-order-with-operator-overloading-functions – Messalina