Reputation: 2746
Just for curious mind. During problem solving many question says,"Input will be terminated by Ctrl+z". I know its "EOF(End Of File)" But...
while(scanf("%d",&a)==1)
{ cout<<"OK"<<endl;}
while(scanf("%d",&a)!=EOF)
{cout<<"OK"<<endl;}
while(cin>>a)
{cout<<"OK"<<endl;}
Above 3 will be terminated by Ctrl+z.
while(scanf("%d",&a))
{cout<<"OK"<<endl;}
It will give OK by pressing Ctrl+z. and
while(1){cin>>a;
cout<<"OK"<<endl;}
Its a infinte loop.
I want to know how Ctrl+z works on a program termination. What is the reason behind it. Please answer in details.
Upvotes: 0
Views: 4806
Reputation: 4113
Ctrl+z does not terminate your program. It also doesn't pause its execution. It's a 0x1A byte that is interpreted by iostream
and stdio
methods to be EOF (end-of-file). After that character is read from the console, nothing is read further and the method that is reading it returns. In the case of iostream
, std::ios::eof()
becomes true.
You would notice in your last case that if you structured it as:
while(cin >> a) { ... }
It would exit like the others.
Upvotes: 2