Thread: differnce between endl and "\n"

  1. #1
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer

    differnce between endl and "\n"

    i'm a newbie to c++.i want to know that is there any differnce between endl,"\n" and '\n'.(although all are used to place the cursor to newline).any help will be appreciated.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    '\n' is a single newline character.
    "\n" is a string containing only a single newline character.
    std::endl is an iostream manipulator that, when inserted into a stream, writes a newline and then flushes the stream.

    Use endl if you want a newline and want to force immediate display of the output (or you want to safeguard against data loss in the face of a crash, i.e. when writing debug information).
    All other times, in particular when your next operation is writing yet another thing, use the newline character directly.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2005
    Forcing a stream to flush is rarely necessary in C++, it is almost always done automatically in the cases that you need it. Because of this I almost always use '\n' and if I really want a flush I just send std::flush to the stream instead of std::endl to make it clear that I'm trying to force a flush specifically.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  2. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 08:56 PM
  3. How to print "\n" or "\t"
    By Joelito in forum C Programming
    Replies: 1
    Last Post: 01-13-2007, 09:22 PM
  4. Sending "\n" as a string?
    By applejustworks in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2006, 06:37 PM
  5. "\n" or endl
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2001, 01:05 AM