Home > Backend Development > C++ > What does endl mean in c++

What does endl mean in c++

下次还敢
Release: 2024-04-26 17:15:23
Original
846 people have browsed it

endl means "end of line" in C and is used to insert a newline character in the output stream and start a new line. Its working principle is: force the output stream buffer to be refreshed. Inserts a newline character and moves the output pointer to the next line.

What does endl mean in c++

endl meaning in C

endl is a keyword in C, used in the output stream Insert a newline character into . It stands for "end of line" and starts a new line in the output.

Usage

endl can be used like a normal function on the standard output stream cout or any other output stream object. The syntax is as follows:

<code class="cpp">output_stream << endl;</code>
Copy after login

Among them, output_stream is the output stream object to which newlines are to be inserted.

How it works

endl forces the buffer of the output stream to be flushed by calling a member function named flush(). This means that any data that has not yet been written to the output device is sent out immediately. Inserting a newline character also means that the output pointer will move to the next line.

Example

The following example uses endl to print "Hello World" in the terminal, and then prints "C " on a new line:

<code class="cpp">#include <iostream>

using namespace std;

int main() {
  cout << "Hello World" << endl;
  cout << "C++" << endl;
  return 0;
}</code>
Copy after login

Output results :

<code>Hello World
C++</code>
Copy after login

Supplementary Note

  • endl is a macro that expands into an expression that performs the above behavior.
  • endl differs from '\n', which is a character escape sequence that inserts a newline character in the output but does not flush the buffer.

The above is the detailed content of What does endl mean in c++. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template