473,757 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"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 3362
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 objektorientier ter Datenverarbeitu ng
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...@at t.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 objektorientier ter Datenverarbeitu ng
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 objektorientier ter Datenverarbeitu ng
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
9033
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' undeclared (first use this function)
11
3250
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? Then, I found, in this book, the semantic of set::iterator is defined as same as set::const_iterator. Both of them must be const! I tried to read the source of GNU STL(version 3.4.1).They were using a red-black tree to implant it (std::set has a...
12
6108
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
6078
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: ************ HelloWorld.cpp ************* #include "stdafx.h" void main()
9
3382
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 World!" << std::cout; -- /Xian
7
3605
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 tho network to a client so that errors and debug messages can be displayed simultaneously anywhere. I tried to use a std::stringstream to do the job. I created the stringstream in main() and pass it as pointer into a few threads. Those threads...
0
9487
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7285
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5324
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.