P
P
ProstoUserName2020-10-09 19:47:07
C++ / C#
ProstoUserName, 2020-10-09 19:47:07

Is it possible to use endl instead of "\n" in C++?

Is it possible to write endl instead of "\n"?
cout << "Hi" << endl << "world" << endl;

endl means the end of the line, but I did not understand what kind of idea it is, about the end of the line which is in the terminal or in the code?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Pavlov, 2020-10-09
@ProstoUserName


They both serve the same endl and \n the same purpose in C++ - they insert a newline. However, the key difference between the two is that endl is cleared each time the output buffer is called, but \n does not.
Anything that needs to be output is first queued in the output buffer and then written to the device (hard drive, monitor, etc.),
clearing the queue in the process.

V
Vladimir Korotenko, 2020-10-09
@firedragon

https://ru.wikipedia.org/wiki/CP866
https://ru.wikipedia.org/wiki/Windows-1251
https://ru.wikipedia.org/wiki/UTF-8
Have a fun weekend
Correction. Posted in the wrong window.
The behavior depends on the operating system and the mode of opening the file
Sorry, I accidentally answered the question about using it in a switch.
In your case, it all depends on the operating system and the file opening mode
Opening a file in text mode should cause std::endl to be converted to the appropriate line ending for your platform. Your problem is that newline is appropriate for your platform, but the files you create aren't intended for your platform.
I'm not sure how you plan on overloading or changing endl, and changing its behavior would certainly be surprising for any developers new to your project. I'd recommend switching to win_endl (should be a simple search-and-replace) or maybe switching from a standard ostream to a Boost.Iostreams filtering stream to do the conversion for you.

T
Timur Pokrovsky, 2020-10-09
@Makaroshka007

Is it possible to write endl instead of "\n"?

Yes, you can
but I didn’t understand what kind of idea we are talking about, about the end of the line which is in the terminal or in the code

In the terminal, of course

W
WAR-S, 2020-10-09
@WAR-S

I allow)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question