c++ - C instruction is being reordered when cin cout and gets is used consecutively -


does knows why c instruction being reordered when cin cout , gets used consecutively here? using dev-c++ 4.9.9.2.

#include<iostream> using namespace std;  int main(){     char a[10],b;     for(;;){         cout<<"\ncin>>b:";         cin>>b;         cout<<"gets(a):";         gets(a);         cout<<"cout<<a<<b:"<<a<<" "<<b<<"\n\n";     } } 

i got output like:

cin>>b:132 gets(a):cout<<a<<b:32 1   cin>>b:465 gets(a):cout<<a<<b:65 4   cin>>b:312242 gets(a):cout<<a<<b:12242 3   cin>>b:1 gets(a):cout<<a<<b: 1  cin>>b: 

it seemed input cin passed in gets.. , appears instructions reordered like:

cin>>b; gets(a); cout<<"gets(a):"; 

instead of,

cin>>b; cout<<"gets(a):"; gets(a); 

cin>>b read character, leaving rest of input read later input operation. gets sill has read , don't block.

at first cin >> b, there no input available. enter '132\n' (input terminal made line line) in buffer , 1 out of it. gets reads next characters 32 , \n terminates gets. doesn't need read more terminal.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -