Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Intermediate
Oct 19, 2004

Difference Between endl and '\n'

Take a look at the following code:

#include <iostream>
int main()
{
  int i = 12;
  std::cout << i << '\n'; // A
  std::cout << i << std::endl; // B
  return 0;
} 
Using '\n' is a request to output a newline. Likewise, using endl requests to output a newline, but it also flushes the output stream. In other words, endl has the same effect as (ignoring the std:: for now):
 
cout << i << '\n'; // C: Emit newline
cout.flush(); // Then flush directly 
Or this:
 
cout << i << '\n' << flush; // D: use flush manipulator 
It's worth pointing out that these are different too:

cout << i << '\n'; // E: with single quotes
cout << i << "\n"; // F: with double quotes 
Note that Es last output request is for a char, hence operator Fs case, the last is a const char[2], and so operator Mohan Bisht
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date