Open In App
Related Articles

std::endl vs \n in C++

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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()
{
 
    // 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!



Last Updated : 19 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads