
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
cout << endl vs cout << “\n” in C++
In this section we will see what are the differences between cout << endl, and cout “\n” in C++. We should use cout << “\n” in different cases, and avoid endl.
So why we should avoid the std::endl while printing lines into console or a file. We use std::endl for creating a newline after the current line. For few lines of IO operations, it is not making any problems. But for large amount of IO tasks, it decreases the performance.
The endl is used to create new lines, but it does not send to the new line only, after sending the cursor to the next line it flushes the buffer each time.
The flushing of buffers is not the programmers task; the operating system is responsible for it. Each time it requests for flushing, it requests to the operating system. This requesting is comparatively expensive. And we don’t really need to flush buffers every time after writing some lines. The IO streams automatically clears the buffer when it is full.
If we analyze the required time to write nearly 100000 lines of texts into file by using std::endl, and using ‘\n’ we can easily see the difference. The code which is using std::endl is taking nearly two times more times to complete the task compared to using ‘\n’ after it.
- Related Questions & Answers
- Why do we use cin >> and cout << in C++ ?
- '></a></li></ul><i>xssi</i>
- Difference between <ion-list> and <div class=“list”> in Ionic framework?
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- How exactly does <script defer=“defer”> work?
- Do you think operator < is faster than <= in C/C++?
- Print all multiplicative primes <= N in C++
- What is JavaScript Bitwise Left Shift(<<) Operator?
- jQuery :lt() Selector
- What is difference between <button> vs. <input type="button" />?
- Overloading stream insertion (<<) and extraction (>>) operators in C++
- What is Bitwise Left Shift Operator (<<) in JavaScript?
- Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in C++
- Should I use <img>, <object>, or <embed> for SVG files?
- IntArray vs. Array<Int> in Kotlin