C++ Question on using endl or newline
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments... -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Monday 03 April 2006 17:18, Jerry Feldman wrote:
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments...
It's not only portability, it's also about buffer flushing: http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 OTOH this might be a reason to advise against std::endl, too: http://llvm.org/docs/CodingStandards.html#hl_avoidendl CU -- Stefan Hundhammer <sh@suse.de> Penguin by conviction. YaST2 Development SUSE Linux Products GmbH Nuernberg, Germany
On Monday 03 April 2006 11:58 am, Stefan Hundhammer wrote:
On Monday 03 April 2006 17:18, Jerry Feldman wrote:
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments...
It's not only portability, it's also about buffer flushing:
http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2
OTOH this might be a reason to advise against std::endl, too:
http://llvm.org/docs/CodingStandards.html#hl_avoidendl I was looking for something like this that is specific and to the point. Some C++ texts teach std::endl, and others teach that using the newline characters. I knew that endl did the buffer flush. The main reason I asked is to get some good definitive answers since I do occasionally teach C++ (although I am primarily a C programmer). -- Jerry Feldman <gaf@blu.org> Boston Linux and Unix user group http://www.blu.org PGP key id:C5061EA9 PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
On Monday, 3. April 2006 17:18, Jerry Feldman wrote:
I've seen a number of documents and code that prefers to use the newline character ("\n" or '\n') rather than std::endl In the general case, the issue is that endl forces a buffer flush where '\n' does not, and therefore using the newline is more efficient. However, using endl is more portable. Either way works fine on Linux. Comments... --
I think the choice is purely if you would like the buffer flush or not. The is no portability issue with '\n': In the ISO standard section 27.6.2.7 std::endl is defined as namespace std { template <class charT, class traits> basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os); } 1 Effects: Calls os.put(os.widen('\n') ), then os.flush(). 2 Returns: os As you can see the line is terminated by outputing the char '\n'. In this case it is first widened. In the case of os << '\n'; it will also be widened as specified in 27.6.2.5.4 Put simply in formatted output streams the char '\n' represents a line termination whatever the underling OS. Michael -- ___________________________________ Michael Stevens Systems Engineering 34128 Kassel, Germany Phone/Fax: +49 561 5218038 Navigation Systems, Estimation and Bayesian Filtering http://bayesclasses.sf.net ___________________________________
participants (3)
-
Jerry Feldman
-
Michael Stevens
-
Stefan Hundhammer