68

Is there a reason to use endl with cout when I can just use \n? My C++ book says to use endl, but I don't see why. Is \n not supported as widely as endl, or am I missing something?

PypeBros
  • 2,607
  • 24
  • 37
Moshe
  • 57,511
  • 78
  • 272
  • 425
  • 6
    To be clear, the vast majority of the time, you should **not** use `endl`. See also [this answer](http://stackoverflow.com/questions/5492380/what-is-the-c-iostream-endl-fiasco/5492605#5492605) + comments. – ildjarn Sep 06 '11 at 19:11
  • remember end-of-line is "\r\n" on a DOS/Windows platform and bare "\r" on some (all?) Mac platform, too. – PypeBros Sep 06 '11 at 19:12
  • 6
    @sylvainulg: That's not particularly relevant. A '\n' character printed to a text stream will be automatically translated to the system's end-of-line representation. There's no difference between `'\n'` and `std::endl` as far as that's concerned (the difference is the `flush`). (Mac platforms, starting with OSX (I think) are Unix-based, and use `'\n'` to mark line endings.) – Keith Thompson Sep 06 '11 at 19:18
  • And if you do need to flush the stream as often as possible (e.g to see debug output before a crash), you could just use `std::cerr` which doesn't need explicit flushing either. – UncleBens Sep 06 '11 at 19:55
  • @keith: thanks for the precision. That wasn't very clear in the documentation I had at hand, and I didn't know myself about the flush difference. – PypeBros Sep 06 '11 at 20:02
  • @UncleBens: Yes, but `std::cerr` could easily go to a different file than `std::cout`. – Keith Thompson Sep 06 '11 at 20:21
  • This link also tells when to use flushing (`\endl`) and when just `\n` will do. http://programmers.stackexchange.com/questions/185064/using-a-stream-manipulator-endl-or-a-newline-escape-character-n – Ruchir Aug 21 '15 at 09:45

3 Answers3

86

endl appends '\n' to the stream and calls flush() on the stream. So

cout << x << endl;

is equivalent to

cout << x << '\n';
cout.flush();

A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of cout you may not notice the difference since it's somehow synchronized (tied) with cin, but for an arbitrary stream, such as file stream, you'll notice a difference in a multithreaded program, for example.

Here's an interesting discussion on why flushing may be necessary.

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 1
    Also, isn't endl converted to the proper plataform EndLine symbol("\r\n" on windows, "\n" on Linux)? – André Puel Sep 06 '11 at 19:11
  • 7
    The C standard specifies that `'\n'` is transparently converted to the appropriate character. So, yes, since `endl` attaches a `'\n'`, this is happening. But its not specific to `endl`. – Chad La Guardia Sep 06 '11 at 19:13
  • Or, more precisely, you could write `(cout << x << '\n').flush();`. That is, all in just one statement now as `cout << x << endl;` is one statement. – Nawaz Sep 06 '11 at 19:14
  • 3
    @Nawaz: Eschew obfuscation and surplusage. Espouse elucidation – Armen Tsirunyan Sep 06 '11 at 19:15
  • 2
    @Chad: Yes -- and so does the C++ standard, which is what we're discussing here. – Keith Thompson Sep 06 '11 at 19:19
  • 1
    @Keith Thompson The fact that the `'\n'` is transparently converted is not explicitly stated in the "C++" standard; it is something that carried over from the C standard. – Chad La Guardia Sep 06 '11 at 19:38
  • @Chad: Hmm, I didn't know that. But in that case, it's important to note that C++ inherits that rule from C. (I'm trying to figure out where the C++ standard says that, but I find the C++ standard harder to read than the C standard.) – Keith Thompson Sep 06 '11 at 19:49
  • 1
    @Keith: There was a question around SO: "What can you do with C that you cannot do with C++". One of my favorite answers read: "I can lift the printed version of the annotated C standard 1000 times with one hand" – Armen Tsirunyan Sep 06 '11 at 19:52
7

endl is more than just an alias for the \n character. When you send something to cout (or any other output stream), it does not process and output the data immediately. For example:

cout << "Hello, world!";
someFunction();

In the above example, there's is some chance that the function call will start to execute before the output is flushed. Using endl you force the flush to take place before the second instruction is executed. You can also ensure that with the ostream::flush function.

Paul Manta
  • 30,618
  • 31
  • 128
  • 208
-2

endl is a function not a keyword.

#include <iostream>
int main()
{
 std::cout<<"Hello World"<<std::endl;  //endl is a function without parenthesis.
 return 0;
}   

To understand the picture of endl you firstly need to understand about "Pointer to Functions" topic.

look at this code (in C)

#include <stdio.h>
int add(int, int);
int main()
{
   int (*p)(int, int); /*p is a pointer variable which can store the address    
   of a function whose return type is int and which can take 2 int.*/
   int x;

   p=add;                     //Here add is a function without parenthesis.

   x=p(90, 10); /*if G is a variable and Address of G is assigned to p then     
   *p=10 means 10 is assigned to that which p points to, means G=10                        
   similarly x=p(90, 10); this instruction simply says that p points to add    
   function then arguments of p becomes arguments of add i.e add(90, 10)   
   then add function is called and sum is computed.*/  

   printf("Sum is %d", x);
   return 0;
}
int add(int p, int q)
{
  int r;
  r=p+q;
  return r;
}

Compile this code and see the Output.

Back to topic...

 #include <iostream>
 //using namespace std; 
 int main()
 {
 std::cout<<"Hello World"<<std::endl;
 return 0;
 }

iostream file is included in this program because the prototype of cout object is present in iostream file and std is a namespace. It is used because defination(library files) of cout and endl is present in namespace std; Or you can also use "using namespace std" at top, so you don't have to write "std::coutn<<....." before each cout or endl.

when you write endl without paranthesis then you give the address of function endl to cout then endl function is called and line is changed. The reason Behind this is

namespace endl
{
printf("\n");
}

Conclusion: Behind C++, code of C is working.