Minimize the Use of the endl Manipulator

Many programmers append the endl manipulator after each cout expression, as in:

 cout << "user login" << endl;cout << "enter your name: " << endl;cin >> name;  

In the first line, the endl manipulator is added to force a line break. However, it’s cheaper in terms of performance to use the ‘
‘ character instead of endl because endl also flushes the stream rather than just adding a line break. Flushing a stream can be a costly operation. The second cout expression too has a redundant endl manipulator because the display must appear on the screen before the cin expression is executed, as cin and cout are tied.

The bottom line: avoid using the endl manipulator if you can because it slows down your program.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: