What is endl in C++?

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

In C++, endl is a manipulator used with output streams like std::cout to insert a newline character (\n) and flush the output buffer. It ensures that the output is immediately written to the console or file, which is particularly useful when dealing with buffered output.

Example of endl Usage:

#include <iostream> using namespace std; int main() { cout << "Hello" << endl; // Outputs "Hello" followed by a newline cout << "World" << endl; // Outputs "World" followed by a newline return 0; }

Key Points:

  • Newline: endl inserts a newline character (\n), moving the cursor to the next line.
  • Buffer Flush: It also flushes the output buffer, meaning the content is immediately sent to the console or file. This is important in situations where you want to ensure that the output is not delayed, like when debugging.

endl vs \n:

  • endl: Inserts a newline and flushes the buffer.
  • \n: Only inserts a newline without flushing the buffer, making it more efficient for performance in scenarios where frequent flushing is not necessary.

In most cases, if you do not need to flush the buffer, using \n is preferred for performance reasons.

Sources:

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What skills are required for app development?
How long are mock interviews?
What are the top coding interview books reddit?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Image
Grokking Data Structures & Algorithms for Coding Interviews
Image
Grokking Advanced Coding Patterns for Interviews
Image
One-Stop Portal For Tech Interviews.
Copyright © 2024 Designgurus, Inc. All rights reserved.