std::endl and \n both seem to do the same thing but there is a subtle difference between them.
std::cout << std::endl inserts a new line and flushes the stream(output buffer), whereas std::cout << “\n” just inserts a new line.
Therefore, std::cout << std::endl; can be said equivalent to std::cout << ‘\n’ << flush;
Though in some use cases like competitive programming, it is better to use “\n” when the problem is not interactive, as it saves time by not flushing the entire line and thus improves the time complexity of the program.
Some other differences between endl and \n are:
endl
|
\n
|
It is a manipulator. |
It is a character. |
It doesn’t occupy any memory. |
It occupies 1 byte memory as it is a character. |
It is a keyword and would not specify any meaning when stored in a string. |
It can be stored in a string and will still convey its specific meaning of line break. |
We cannot write ‘endl’ in between double quotations. |
We can write ‘\n’ in between double quotations like std::cout<<“\n”; |
It is only supported by C++. |
It is supported in both C and C++. |
It keeps flushing the queue in the output buffer throughout the process. |
It flushes the output buffer only once at the end of the program |
Note: std::cout << “\n” looks performance wise better but in real std::cout << std::endl is much better in C++; As it doesn’t occupies any memory and also if flushing of stream is required.
Explanation for the last point of difference :
Suppose you have a C++ program that writes some output to the console using the std::cout stream. You want to print the message “Hello World” followed by a new line character.
If you write the following code:
C++
#include <iostream>
int main() {
std::cout << "GFG! \n" ;
}
|
The output buffer will not be flushed immediately, and the message will be stored in the buffer until the program finishes. This can be more efficient if you’re writing a lot of output to the console, because flushing the buffer can be an expensive operation.
However, if you write the following code instead:
C++
#include <iostream>
int main() {
std::cout << "GFG!" <<std::endl;
return 0;
}
|
The output buffer will be flushed immediately, which means that the message “Hello World” will be written to the console right away. This can be useful if you want to make sure that the output is displayed immediately, for example if you’re writing a progress indicator or a status message.
In summary, using std::endl in C++ will flush the output buffer and write the data to the output device immediately, while using \n will not flush the output buffer until it is necessary or manually triggered.
Example 1:
We can use std::endl in C++ but not in C. So std::endl runs well in C++ but if we use C, it runs error.
C++
#include <iostream>
int main()
{
std::cout << "GFG!" << std::endl;
std::cout << "GFG!" ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
printf ( "GFG!" , endl);
printf ( "GFG!" );
return 0;
}
|
Output:
GFG!
GFG!
Example 2:
We can use “\n” in both C and C++ but it occupies 1 byte memory.
C++
#include <iostream>
int main()
{
std::cout << "GFG!"
<< "\n" ;
std::cout << "GFG!" ;
return 0;
}
|
C
#include <stdio.h>
int main()
{
printf ( "GFG!\n" );
printf ( "GFG!" );
return 0;
}
|
Output:
GFG!
GFG!
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!