-1
#include <iostream>
using namespace std;

int main()
{   
    ios::sync_with_stdio(0);
    cin.tie(0);

    cout << "Print two numbers: ";
    int x, y;
    cin >> x >> y;
    cout << x << y;
    
    return 0;
}

Input : 23 24 

Output : Print two numbers: 2324

Here before printing the line "Print two numbers: " on console, stdin is waiting for x and y, then prints the entire output on console as given above.

After removing sync lines from above code:

#include <iostream>
using namespace std;

int main()
{   
    // ios::sync_with_stdio(0);
    // cin.tie(0);

    cout << "Print two numbers: ";
    int x, y;
    cin >> x >> y;
    cout << x << y;
    
    return 0;
}

Here first it's printing line "Print two numbers: " on console then stdin stream is waiting for x and y.

I am unable to understand this behaviour.

tusharRawat
  • 719
  • 10
  • 24

1 Answers1

2

By default, cin is tied to cout, so operations on those streams are in the order they are written in the program.

However, doing cin.tie(0) will untie cin from cout, so operations on cout and cin may be interspersed.

Note that all operations on any one stream will still be in the order they are written in the program.

cigien
  • 57,834
  • 11
  • 73
  • 112