Question:
I have a question with the endl
function of the stream handler.
According to the book I'm reading (Deitel), endl
the output buffer.
What does this mean? Since if I put "\n"
or std::endl
at the end of cout
I get the same output.
Is there any way I can see the difference?
Answer:
endl
is used to generate endl
:
Is there a way to see the difference? To answer your question I leave you an example:
#include <iostream>
using namespace std;
int main() {
// your code goes here
std::cout<<"hola1" << std::endl;
std::cout<<"hola2" << std::endl;
std::cout<<"hola3";
std::cout<<"hola4";
return 0;
}
the result will be:
hola1
hola2
hola3hola4
hola3
that for hola3
and hola4
did not use endl
endl
has the same result as \ n, the only difference is that std::endl
empties the output buffer, and '\ n' does not, that is, if you want to force an output. use endl
the same example but with \n
:
#include <iostream>
using namespace std;
int main() {
// your code goes here
std::cout<<"hola1\n";
std::cout<<"hola2\n";
std::cout<<"hola3";
std::cout<<"hola4";
return 0;
}