Why use endl when I can use a newline character? [duplicate]

懵懂的女人 提交于 2019-12-18 09:59:45

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/7324843/why-use-endl-when-i-can-use-a-newline-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!