Search code examples
c++synchronizationstdio

Why using cin before sync_with_stdio(false) give random values to next i/p variable


This is a simple code:

#include <iostream>
using namespace std;

int main() {
ios::sync_with_stdio(false);
int t,n;cin>>t;

cin.tie(NULL);


while(t--){

    cin>>n;
    cout<<n<<endl;
}
}

If I give input as

2
1
2

I get ouput 1 2(in new line) as expected. Now if I use cin>>t before ios::sync_with_stdio(false);. Then for the same i/p, o/p is 0 0(in new line).

AFAIK: ios::sync_with_stdio(false); stops the sync with c++ and c i/p o/p, but I am using cin in both the cases, why it is not working?


Solution

  • this behavior is implementation-defined.

    If this function(Here it refers to sync_with_stdio) is called after I/O has occurred on the standard stream, the behavior is implementation-defined: implementations range from no effect to destroying the read buffer.