Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

cout vs endl in C++



In C++, both count << endl and count << "\n" are used to insert a new line in the output. But they have an important difference:

COUT << ENDL;

Following are the important point of count << endl;

  • Inserts a newline character (\n) and flushes the output buffer.
  • Flushing means it forces all output to be written immediately.
  • Slower than "\n" in performance-critical situations due to flushing.
  • Useful when you want to make sure output appears immediately (e.g., debugging, console logs).

Let's see syntax of the count << endl ?

cout << "tutorialspoint" << endl;

COUT << "\n";

Following are the important point of count << "\n";

  • Inserts only a newline character, without flushing the output buffer.
  • Faster and preferred when flushing is unnecessary.
  • More suitable in performance-sensitive code like competitive programming.

Let's see syntax of the count << "\n" ?

cout << "tutorialspoint \n";

Which One Should be Used?

Use "\n" for better performance in competitive programming. Otherwise, use endl to flush the output and help with debugging.

Updated on: 2025-05-20T18:50:02+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements