What is the difference between cout << endl; and cout << '\n';

Are they absolutely identical from the runtime point of view?
Nusrat NuriyevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Yes and no. The difference is that 'endl' will additionally call 'flush()' on the stream:

		// MANIPULATORS
template<class _Elem,
	class _Traits> inline
	basic_ostream<_Elem, _Traits>&
		__CLRCALL_OR_CDECL endl(basic_ostream<_Elem, _Traits>& _Ostr)
	{	// insert newline and flush stream
	_Ostr.put(_Ostr.widen('\n'));
	_Ostr.flush();
	return (_Ostr);
	}

Open in new window

Nusrat NuriyevAuthor Commented:
okay, then maybe I will rephrase my question, is there any portability issues? How windows and linux knows that on windows 0x0A, and on linux 0x0D0A?
jkrCommented:
Well, by implementing it the right way ;o)

The above was taken from VC++'s 'ostream'. g++ will use sth. like '_Ostr.put(_Ostr.widen("\r\n"));' instead. Would love to post the relevant snippet from g++'s headers, but unfortunately this machine doesn't have them :-/
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

jkrCommented:
Argh, no. I knew I had g++ here, and it loks like this:

  // [27.6.2.7] standard basic_ostream manipulators
  /**
   *  @brief  Write a newline and flush the stream.
   *
   *  This manipulator is often mistakenly used when a simple newline is
   *  desired, leading to poor buffering performance.  See
   *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
   *  on this subject.
  */
  template<typename _CharT, typename _Traits>
    basic_ostream<_CharT, _Traits>&
    endl(basic_ostream<_CharT, _Traits>& __os)
    { return flush(__os.put(__os.widen('\n'))); }

Open in new window


But: Your fault ;o), you got me all confused, since it's exactly the other way round - Windows uses '\r\n' and UN*X only '\n'.

Windows takes care of that one level 'below', in the CRT. This is controlled via '_setmode()' (https://msdn.microsoft.com/en-us/library/tw4k6df8.aspx), or in a nutshell:

The _setmode function sets to mode the translation mode of the file given by fd. Passing _O_TEXT as mode sets text (that is, translated) mode. Carriage return–line feed (CR-LF) combinations are translated into a single line feed character on input. Line feed characters are translated into CR-LF combinations on output. Passing _O_BINARY sets binary (untranslated) mode, in which these translations are suppressed.
jkrCommented:
BTW, just in case I was not clear enough: Both implementations only insert a '\n' into the afected stream, but the underlying Windows CRT will translate that into a '\r\n' combination unless set otherwise (and believe me, I had to deal with these on my side unwanted translations a lot)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
>> is there any portability issues?

Just expanding on jkr's previous comment (and please, do not accept this as an answer since he has already provided that for you with his earlier comments):

C++ always uses a linefeed (\n) as its line terminator for text, but the underlying platform runtime will then perform linefeed conversation, to the platform native format, when reading/writing on a stream that is open in text mode. This is the primary difference between opening a stream as text and opening it as binary.
Nusrat NuriyevAuthor Commented:
Thank you guys! :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.