What does cout<<' '<<f(x); display here?
Asked Answered
A

0

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?

Aube answered 1/6, 2022 at 8:6 Comment(14)
Don't use bits/stdc++.hTolmach
Use a debugger to step through your code.Prolactin
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).Staw
you could replace the function with void f() { std::cout << "foo"; } for the same effect with much simpler codeYogurt
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);Yogurt
... or read this #4176828Yogurt
its not actually the right duplicate, trying to find the right one...Yogurt
@Staw is it actually specified in C++17? Rule 11 on cppreference's evaluation order page states no difference in C++17...Tolmach
@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.Staw
After C++17, msvc acts the same like gcc and clang. DemoWhittle
@Staw 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..Tolmach
@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) saysStaw
@Staw 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...Tolmach
@Eljay no, the rules for << apply here: #38502087 and https://mcmap.net/q/732851/-c-17-evaluation-order-with-operator-overloading-functionsMessalina

© 2022 - 2024 — McMap. All rights reserved.