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 occupy 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()
{
// We can use endl in C++ and it doesn't
// occupy any memory
std::cout << "GFG!" << std::endl;
std::cout << "GFG!";
return 0;
}
// Code submitted by Susobhan AKhuli
C
#include <stdio.h>
int main()
{
// We can't use endl in C
printf("GFG!", endl); // So it runs error
printf("GFG!");
return 0;
}
// Code submitted by Susobhan AKhuli
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;
}
// Code submitted by Susobhan AKhuli
C
#include <stdio.h>
int main()
{
printf("GFG!\n");
printf("GFG!");
return 0;
}
// Code submitted by Susobhan AKhuli
Output:
GFG!
GFG!
Similar Reads
std::find_end in C++
std::find_end is used to find the last occurrence of a sub-sequence inside a container. It searches the range [first1,last1) for the last occurrence of the sequence defined by [first2,last2), and returns an iterator to its first element, or last1 if no occurrences are found. It is similar to std::se
6 min read
std::endian in C++ 20
In C++ programming, there's a new addition in C++20 called std::endian. It's a helper that makes dealing with the way computers store data in memory much simpler. In this article, we will discuss the std::endian and how to use it in C++ program. What is Endianness?Before we look at the std::endian,
2 min read
list end() function in C++ STL
The list::end() is a built-in function in C++ STL which is used to get an iterator to past the last element. By past the last element it is meant that the iterator returned by the end() function return an iterator to an element which follows the last element in the list container. It can not be used
2 min read
std::count() in C++ STL
In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++. Letâs take a quick look at a simple example that uses count() method:
3 min read
std::string class in C++
C++ has in its definition a way to represent a sequence of characters as an object of the class. This class is called std:: string. The string class stores the characters as a sequence of bytes with the functionality of allowing access to the single-byte character. String vs Character ArrayString Ch
8 min read
C++ Std vs Stl
The full form of std is standard and it is a namespace. All the identifiers are declared inside the std namespace, in other words, a namespace provides scope to identifiers such as function names, variable names, etc. defined inside it. It is a feature especially available in C++ and is not present
4 min read
std::string::insert() in C++
In C++, the string::insert() function is used insert the characters or a string at the given position of the string. It is the member function of std::string class. The string::insert method can be used in the following ways to: Table of Content Insert a Single CharacterInsert a Single Character Mul
4 min read
Vector emplace() vs insert() in C++
In C++, STL vector provides two methods for element insertion at the given position: vector emplace() and vector insert(). Although both methods provide similar functionality, there are a difference regarding how they work. The following table lists the primary differences between the vector emplace
3 min read
std::string::append() in C++
The string::append() in C++ is used append the characters or the string at the end of the string. It is the member function of std::string class . The string::append() function can be used to do the following append operations: Table of Content Append a Whole StringAppend a Part of the StringAppend
3 min read
getline (string) in C++
The C++ getline() is a standard library function that is used to read a string or a line from an input stream. It is a part of the <string> header. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered
5 min read