endl vs in C++

Although they both seem to do the same thing, there is a subtle difference between them.

cout << endl  : Inserts a new line and flushes the stream

cout << "
"  : Only inserts a new line.


cout << endl;

can be said equivalent to

cout << ‘ ’ << flush;

So cout << “ ” seems performance wise better than cout << endl; unless flushing of stream is required.

Some other differences between endl and are:

  1. endl is manipulator while is character.
  2. endl doesn’t occupy any memory whereas is character so It occupy 1 byte memory.
  3. We cannot write endl in between double quotation while we can write in between double quotation like
    cout<<" "; it is right but cout<<"endl"; is is wrong.
  4. We can use also in C++ but we can't use endl in C language.


This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment



load comments

Subscribe to Our Newsletter