472,992 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

"std::endl or "\n" "

hi,
whts the difference between the std::endl or "\n" ?
because both do the same work

Thanks
Mar 29 '08 #1
9 3289
On Mar 29, 12:48*pm, andrew.smith....@gmail.com wrote:
hi,
whts the difference between the std::endl or "\n" *?
because both do the same work

Thanks
I found nothing difference between both at least in my program.
I also want to know that.
Mar 29 '08 #2
On 29 mar, 05:48, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
std::endl flushes the buffer, "\n" doesn't. If you don't know
what you're doing, and don't want to think about it, use
std::endl. It will make debugging a lot easier. If you're
outputting a lot of short lines in a sequence, however, you
probably want to use "\n", since flushing can be a fairly
expensive proposition. (My general rule is to use "\n" in loops
or short sequences of code, but to make sure that there is an
std::endl or an explicit call to flush at the end of the
function or after the loop.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 29 '08 #3
On 29 mar, 07:22, pauldepst...@att.net wrote:
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
Which is a very visible difference if your program crashes
immediately after, or if you don't do anything to flush the
buffer immediately.

Ideally, you'd consider both and choose the appropriate one each
time. Practically, for most smaller programs, just using
std::endl everywhere is the simplest solution. (If you're
generating a file of a couple of million lines, however, I
wouldn't recommend it.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 29 '08 #4
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
Mar 29 '08 #5
On Sat, 29 Mar 2008 07:03:29 -0700, André Castelo wrote:
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ? because both
do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.

What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
When you flush the stream buffer you're telling it to write what
ever it holds and clear it. If it doesn't get flushed, you won't see it
on your terminal or in your file.

--
OU
Mar 29 '08 #6
André Castelo wrote:
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.

What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
No. Writing I/O is usually faster if you write several bytes at once instead
of sending each to its target seperately, be it for file I/O or the
standard output streams, since there is (on most systems) a relatively
large fixed overhead for writing something. Therefore, the streams buffer
data internally and then send it to its target as a block, either when the
buffer is full, or when you explicitly flush the stream.

Mar 29 '08 #7
André Castelo wrote:
What are the advantages of flushing the stream buffer?? Does it mean
that the ocasional '\n' from input will be discarded or am i missing
something?
If you are, for example, writing thousands of lines of text to a file,
using "\n" instead of std::endl is much faster because the stream is not
flushed after each line.

If you are printing a few lines of text to standard output, std::endl
may be a better idea because it forces the text to be flushed before the
program continues.
Mar 30 '08 #8
André Castelo a écrit :
On Mar 29, 12:48 pm, andrew.smith....@gmail.com wrote:
whts the difference between the std::endl or "\n" ?
because both do the same work
Not all differences are visible. The difference is that std::endl
flushes the stream's buffer and \n doesn't.
What are the advantages of flushing the stream buffer?? Does
it mean that the ocasional '\n' from input will be discarded
or am i missing something?
The data isn't actually output until you flush the buffer. If
someone else is waiting for it, they wait. If your program
crashes, it never gets written.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Mar 30 '08 #9
James Kanze wrote:
The data isn't actually output until you flush the buffer.
Except if the buffer gets full. (Or you close the file handle, or...)
Mar 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
11
by: snnn | last post by:
On the book <Generic Programming and the STL>( Matthew . H . Austern ),this function is defined as iterator set::begin() const. However, why should a const object returns a non-const iterator?...
12
by: Filipe Sousa | last post by:
Hi! Could someone explain to me why this operation is not what I was expecting? int main() { int x = 2; std::cout << x << " " << x++ << std::endl; return 0; }
9
by: Richard Lionheart | last post by:
Hi All, I've got Visual Studio .Net installed, but I don't know it very well. So I tried to create a plain old Win32 using the command-line complier. I tried to compile: ************...
9
by: Xian | last post by:
Is there a right and proper (tm) way to end a line? Thinking about portability and the mess that the 'wrong' line endings can cause. E.g. std::cout << "Hello World!\n"; or std::cout << "Hello...
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.