PDA

View Full Version : "\n" VS <<endl;



RoD
11-07-2002, 08:07 AM
Recently the idea of which one is better has arose on both the boards in one of my posts, and in my class. Both told me i shouldn't use "\n" but rather use <<endl;

What is your opinion?

Magos
11-07-2002, 08:28 AM
ostream _FAR & _RTLENTRY _EXPFUNC endl(ostream _FAR &); // insert newline and flush
ostream _FAR & _RTLENTRY endl(ostream _FAR &); // insert newline and flush

Seems like endl both breaks the line and flushes the buffer.

Personally, I use endl in C++. Not that I know it's better, but cause I've always done so :).
Plus, it looks better with ... << endl << ...
than ... << "\n" << ...

I suppose you could do:

#define endl "\n"

or:

#define endl "\n" << flush

RoD
11-07-2002, 08:33 AM
So in other words, endl; would me much more effecient in a lengthy program and is better practice?

Sentaku senshi
11-07-2002, 09:04 AM
I always just use both. I use \n when the end is in quotes

cout<<somevar<<"Text in quotes \n" ;

and endl when it is not

cout<<"Text in quotes"<<someVar<<endl;

Monster
11-07-2002, 09:48 AM
Originally posted by Magos
Personally, I use endl in C++. Not that I know it's better, but cause I've always done so :).

Dito

Prelude
11-07-2002, 09:49 AM
>So in other words, endl; would me much more effecient in a lengthy program and is better practice?
Actually, endl would probably be less efficient per call than \n. But that kind of performance nitpicking is a bad thing in most programs, so I just use endl if I need to flush the stream.

If I need a single newline and flush:
std::cout<<std::endl;

If I need two newlines and a flush:
std::cout<<'\n'<<std::endl;

If I need a newline, flush not needed:
std::cout<<'\n';

-Prelude

RoD
11-07-2002, 10:23 AM
How do you know when you do(n't) need to flush the line?

Magos
11-07-2002, 11:04 AM
Originally posted by Ride -or- Die
How do you know when you do(n't) need to flush the line?
I might be wrong, but:

It is when you flush the buffer that the text actually appears on the screen. If you print:

cout << "Hello" << flush << "World" << flush;

"Hello" would be printed on the screen a little bit before "World", causing flicker (probably unnoticable to a human eye, but still...).

RoD
11-07-2002, 11:12 AM
ok what mod 999'd the poll :P

kermi3
11-07-2002, 11:22 AM
What are you talking about ROD?

RoD
11-07-2002, 11:23 AM
thats it i'm going to start taking screenshots!

Magos
11-07-2002, 11:28 AM
LOL! :D

kermi3
11-07-2002, 11:31 AM
Screen shots? Are you implying that you don't trust the people and mods here at CP.com? Are you halucinating again? Mayeb you should talk to someone proffessionally about this ROD, wouldn't want anything to happen to you, like loosing it for example.

RoD
11-07-2002, 11:49 AM
..........

Cgawd
11-07-2002, 03:26 PM
endl because it not only skips to the next line but clears the buffer, keeping things nice and neat

Klinerr1
11-09-2002, 01:48 PM
newbie alert.

/n is the same thing as endl he was aksing a prefrence the onyl differ is that you put /n inside the quotes endl is a seperate thing

/n example->

cout << "hello mom /n";
cout << "hello dad";
endl example->

cout << "hello mom" << endl;
cout << "hello dad";
the end resualt sshould be the same

Magos
11-09-2002, 02:27 PM
Originally posted by Klinerr1
newbie alert.

/n is the same thing as endl he was aksing a prefrence the onyl differ is that you put /n inside the quotes endl is a seperate thing

/n example->

cout << "hello mom /n";
cout << "hello dad";
endl example->

cout << "hello mom" << endl;
cout << "hello dad";
the end resualt sshould be the same
No, as was stated before:

endl != \n
endl == \n + Flush

Cgawd
11-09-2002, 02:47 PM
yah, whos the newb now punk ;)

correlcj
11-09-2002, 05:34 PM
<<endl;
simply cause i get lazy and its easier for this ol' fart to type that than other.
PEACE OUT!
cj

Klinerr1
11-09-2002, 07:46 PM
:eek: :eek: :eek: :eek: :eek: :eek: ah well as someone with tons of brain power once said "learn something new a day"

Cgawd
11-09-2002, 07:47 PM
lol if they had tons of brain power, they might have used a lil grammar and said "Learn something new every day"

Shade
11-11-2002, 01:18 AM
i use newl - which simply returns '\n'

I read some performance issues on gcc2.95
in which '\n' was 60 times faster than endl - so I thought, why use endl??

a flush is made when the buffer is full - in most cases that's enough.

Only sometime (if I write to files) it is important to flush, because the program may crash and then the file would be empty...

ammar
11-11-2002, 01:37 AM
I don't think you can say which is better because they are different.
Simply if you need to flush the buffer use endl, if you don't use '\n'.
it's that simple.

DavidP
11-11-2002, 03:56 PM
I have never really had much of a need to flush the buffer, so I just use them like this:

If it is a string, use \n, if it is after a variable, use endl.

Ex:

cout << "Hello, World! \n";

cout << myVar << endl;

Even though endl flushes the buffer, that is not taught in schools. The only thing that is taught in schools is that it goes to the next line, and so that is what people use it for. That is what I have always used it for, and so that's how I use \n and endl.

BMJ
11-11-2002, 03:57 PM
Originally posted by ammar
I don't think you can say which is better because they are different.
Simply if you need to flush the buffer use endl, if you don't use '\n'.
it's that simple. but..... I thought if you wanted to flush something (which solves all IO problems) you use fflush(stdin); Am I mistaken?


:D :D :D

RoD
11-11-2002, 04:05 PM
i'll use endl then...

btw, newb alert yourself :P

Sebastiani
11-11-2002, 04:35 PM
All this discussion and noone has mentioned that stdout is unbuffered, hence no need for endl unless couting to a file, no?

moi
11-11-2002, 04:44 PM
Originally posted by Sebastiani
All this discussion and noone has mentioned that stdout is unbuffered, hence no need for endl unless couting to a file, no?

but how will you know if you're outputting to a file or to a screen or whatever? all you know is that it's stdout.

Sebastiani
11-11-2002, 10:35 PM
Well, obviously. But in most cases, stout is directed to a terminal, and anyway, if otherwise, the programmer would know in advance (hopefully!).

no-one
11-12-2002, 01:15 AM
when i use C++ Ostreams which is very rare i use 'endl', other wise i use '\n'.

>All this discussion and noone has mentioned that stdout is unbuffered

since when? i think this is imp dependant.

moi
11-12-2002, 04:34 AM
Originally posted by Sebastiani
Well, obviously. But in most cases, stout is directed to a terminal, and anyway, if otherwise, the programmer would know in advance (hopefully!).

not even close. you never used linux much i assume (windoze users dont do this sort of thing much), but stdin and stdout are regularly redirected all over the place

Prelude
11-12-2002, 09:43 PM
no-one, I have to know where you get those avatars. The dancing dog was hilarious and the panda is totally cute. :)

-Prelude

ammar
11-13-2002, 12:25 PM
I agree with Prelude on the avatar.
And one more thing, Some times the buffer is automaticaly flushed, for example when you write:
cin >> "blabla";
the buffer is automaticaly flushed.

no-one
11-16-2002, 03:48 AM
>I have to know where you get those avatars.

trade secrets.