C++ endl: Newline & Stream Flush Explained

In C++, endl is a manipulator used to insert a newline character and flush the stream in an output stream. It is typically used with an output stream object to end a line and flush the output buffer to the output device. The function of endl is similar to outputting a newline character \n, but it also forces the output buffer to flush.

Here is how to use endl:

#include <iostream>

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

In the example above, std::endl is inserted into the std::cout output stream to insert a newline and flush the output buffer after printing two lines of text.

bannerAds