differnce between endl and "\n"

This is a discussion on differnce between endl and "\n" within the C++ Programming forums, part of the General Programming Boards category; i'm a newbie to c++.i want to know that is there any differnce between endl,"\n" and '\n'.(although all are used ...

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

    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
    Posts
    8,892
    '\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!
    CornedBee

    "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
    Posts
    7,317
    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, 01:43 PM
  2. Blank lines "\n"
    By Coding in forum C++ Programming
    Replies: 15
    Last Post: 02-18-2008, 07:56 PM
  3. How to print "\n" or "\t"
    By Joelito in forum C Programming
    Replies: 1
    Last Post: 01-13-2007, 08: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, 12:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21