Confusing output of this code

Let me apologize in advance for choosing this unspecific title. I had problems finding a good one, since i dont really get what the problem here is.

So given this code:

int fa()
{
cout << "test";
return 3;
}


int main()
{
cout << "Hello" << fa() << endl;
}

it outputs this:

testHello3

and i dont know why? I expected it to output: Hellotest3
since "Hello" comes first in order after cout. I guess i'm having a false understanding of the cout behavior, but why?
It's nothing to do with cout specifically.
https://en.cppreference.com/w/cpp/language/eval_order

Whether "Hello" is output before or after calling fa() is unspecified.
stream insertion (<<) is 'turned' into a function call for operator<<() - hence the importance of the eval_order info in the above link.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;

string fa()
{
   return "test3";
}

int main()
{
   cout << "Hello" << fa() << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int f3()
{
   std::cout << " test3 ";
   return 3;
}

int f4()
{
   std::cout << " test4 ";
   return 4;
}

int main()
{
   std::cout << f4() << f3() << " Hello " << f3() << f4() << '\n';
}
Topic archived. No new replies allowed.