0
#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?

Andrei
  • 19
  • 3
    [Don't use `bits/stdc++.h`](https://stackoverflow.com/q/31816095/17862371) – Jakob Stark Jun 01 '22 at 08:09
  • Use a debugger to step through your code. – Jason Jun 01 '22 at 08:10
  • 9
    Your book is wrong, the correct answer is "it's unspecified what the output will be" (at least until C++17, since C++17 it is specified and should always produce the same output you see on your computer). – Yksisarvinen Jun 01 '22 at 08:11
  • 4
    you could replace the function with `void f() { std::cout << "foo"; }` for the same effect with much simpler code – 463035818_is_not_an_ai Jun 01 '22 at 08:12
  • 5
    c++ teaching can be weird. They teach you raw pointers because container are considerd "advanced" but on the other hand they confront you with obtuse corner cases that can be easily avoided. Simply write `auto x = f(8174); std::cout << ' ' << x;` or `std::cout << ' '; std::cout << f(8174);` – 463035818_is_not_an_ai Jun 01 '22 at 08:19
  • ... or read this https://stackoverflow.com/questions/4176328/undefined-behavior-and-sequence-points – 463035818_is_not_an_ai Jun 01 '22 at 08:20
  • its not actually the right duplicate, trying to find the right one... – 463035818_is_not_an_ai Jun 01 '22 at 08:22
  • @Yksisarvinen is it actually specified in C++17? Rule 11 on [cppreference's evaluation order page](https://en.cppreference.com/w/cpp/language/eval_order#Rules) states no difference in C++17... – Jakob Stark Jun 01 '22 at 08:25
  • 4
    @JakobStark To be fair, I never bothered with actually understanding the rules, I try to write code that doesn't rely on that. But it seems that rule 19) is the one that adds sequence point, so rule 11) doesn't apply. – Yksisarvinen Jun 01 '22 at 08:30
  • 1
    After C++17, msvc acts the same like gcc and clang. [Demo](https://godbolt.org/z/hP9hasoTx) – Louis Go Jun 01 '22 at 08:32
  • @Yksisarvinen Yeah rule 19) adds a sequence point between the evaluation of the shift operators. So the `' '` is guaranteed to be written first (in C++17). But the output of the return value of `f(8174)` and the side effect output in the recursive calls of `f` should be undetermined if I read correctly.. And of course I support writing code that does not rely on the exact rules.. – Jakob Stark Jun 01 '22 at 08:33
  • @JakobStark I see what you mean, but that would be quite game-breaking bug if it was unsequenced. Function body must be executed (together with its side effects) before transferring control back to the caller. I think that would be what rule 3) says – Yksisarvinen Jun 01 '22 at 08:42
  • @Yksisarvinen gash I'm completely lost in all those rules. I think rule 3) only is about function paramters, but again there is rule 2), which however excludes the side effects. I'm really unsure about this... – Jakob Stark Jun 01 '22 at 08:46
  • 1
    @Eljay no, the rules for `<<` apply here: https://stackoverflow.com/questions/38501587/what-are-the-evaluation-order-guarantees-introduced-by-c17 and https://stackoverflow.com/a/46408392/3684343 – mch Jun 02 '22 at 06:31

0 Answers0