Output buffer for terminal is getting flushed in case of both new line and endl. Why?

31 Views Asked by At

I know that new line character does not flush the output but endl does.

Flushing the buffer means that everything stored in the buffer will be sent to the file which in this case is terminal.

"it clear the buffer by outputting everything in it"

Let's take a reproducible example where I am throwing exception from main program for the runtime to catch.

int main()
{
    cout<<"ABC";
    throw 0;
}

The above works as expected and I don't see ABC on terminal.

int main()
{
    cout<<"ABC"<<endl;
    throw 0;
}

The above prints ABC which is also fine since we know that endl will flush everything.

But what I don't understand is since \n does not flush to terminal, the ouput behvior in the below program should work same as the first one. But NO this prints ABC on terminal. Doesn't this violate what we read, that \n does not flushes the buffer?

int main()
{
    cout<<"ABC"<<"\n";
    throw 0;
}
0

There are 0 best solutions below