(C++) std::endl

March 10, 2018 · View on GitHub

std::endl is an output stream modifier to go to the next line and flush the stream's buffer.

#include <iostream>  

int main() 
{   
  //Go to next line and flush the std::cout buffer   
  std::cout << std::endl; 
}

The code above is equivalent to the code below [2]:

#include <iostream>  

int main() 
{   
  //Go to next line   
  std::cout << '\n';   
  //Flush the std::cout buffer   
  std::cout.flush(); 
}

Advice

  • Avoid std::endl [1,3], as one does not need to flush the std::cout buffer after every output [2].

References