C++

What Does endl Mean in C++

The endl is a keyword in C++ that stands for the end line. It is commonly used in programs to move the cursor to a new line after outputting text or data to the console. This article covers endl keyword and how it maintains the formatting style of program output in C++.

Table of Contents

1. What is endl in C++

The endl is a keyword in C++ that stands for end line. It is used to end a line of output in a console program. The endl keyword is included in the iostream library, which is used to read and write data to and from files or other input/output devices.

2. How Does endl Work

When endl is used in a C++ program, it moves the cursor to the next line of the console output. This is important when you want to output multiple lines of text or data to the console. Without endl, all the output would be displayed on a single line.

3. Why is endl Important in C++ Programming

The endl is important in C++ programming because it allows us to format console output in a readable and organized way. It is often used in conjunction with the insertion operator (<<) to output text or data to the console.

4. Examples of Using endl in C++ Programming

Now we will cover endl use in C++ using different examples and explain how it can be used with different formatting styles.

4.1. Outputting Text with endl

Here is an example of using endl to output text to the console:

#include <iostream>
int main() {
    std::cout << "Hello, world!" << std::endl;
    std::cout << "This is a new line." << std::endl;
    return 0;
}

This program will output the following to the console:

4.2. Outputting Variables with endl

The endl can also be used to output variables to the console. Here is an example:

#include <iostream>
int main() {
    int x = 5;
    std::cout << "The value of x is: " << x << std::endl;
    return 0;
}

This program will output the following to the console:

4.3. Using endl in Loops

The endl can also be used in loops to output multiple lines of text or data to the console. Here is an example:

#include <iostream>
int main() {
    for (int i = 0; i < 5; i++) {
        std::cout << "Line " << i + 1 << std::endl;
    }
    return 0;
}

This program will output the following to the console:

Conclusion

The endl keyword in C++ moves the cursor to the next line of console output. It is a part of the C++ standard library to format the output which as a result improves the readability. The key advantage of using endl is that it flushes the output.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.