473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

\n and endl: What's the difference?

lotus18
866 512MB
Hello I'm a newbie in c++

Can anyone tell me what's the between the two:

1. \n and endl?
2. = and ==?
Nov 19 '07 #1
9 2085
Ganon11
3,652 Expert 2GB
1) Depends on your compiler, I imagine.

2) = is assignment, == is comparison.
Nov 19 '07 #2
lotus18
866 512MB
1) Depends on your compiler, I imagine.

2) = is assignment, == is comparison.
1. I am using MS Visual C++ 6.0. What compiler am I using?

Thanks for your reply : )
Nov 19 '07 #3
gpraghuram
1,275 Expert 1GB
1. I am using MS Visual C++ 6.0. What compiler am I using?

Thanks for your reply : )
If u are using Microsoftt VC++ then the compiler is microsoft compiler which can be called from command prompt using command cl.
Other famous compilers are gcc,g++ etc...
Nov 19 '07 #4
coffear
20
Hello I'm a newbie in c++

Can anyone tell me what's the between the two:

1. \n and endl?
2. = and ==?

As stated = is an assignment but == is a comparison. Be very careful not to get the 2 mixed up. For example if you have:-

while(a=b)
{
execute code here
}

you will end with with an endless loop as a=b will always evaluate to being true (unless for some reason the assignment failed.

With regards to the \n and endl question. I believe \n is platform specific but endl (of course depending on compiler) should work for all platforms.
Nov 19 '07 #5
Ganon11
3,652 Expert 2GB
No, '\n' is a universally accepted escape character to print a newline. I just think that how '\n' and endl are implemented varies from compiler to compiler.
Nov 19 '07 #6
oler1s
671 Expert 512MB
‘\n’ refers to a newline. A newline is actually OS specific. This is where it gets a bit confusing. People will refer to the \n and \r characters independently (Windows is \r\n , *NIX is \n, and old Macs are \r ). But in C++, ‘\n’ is a portable equivalent to whatever the OS considers a newline.

endl is a manipulator function. It does two things effectively. It first inserts a newline into the stream. If the stream is buffered, it will flush it. Which is why if all you want is a newline, insert a ‘\n’. I realize endl is ubiquitously used by beginners because that is what books use. But endl is a function. ‘\n’ is a character.

= and == has already been discussed.

Lotus, your compiler is VC++ 6. It’s old. Get something newer than that. You can get the following for free: gcc for Windows (known as MinGW), Visual C++ 2005 Express Edition. I’m not entirely sure about the VS 2008 betas. Whatever the case, your old compiler will limit you in learning advanced C++. Although it’s perfectly fine for plain C.

EDIT: So I should clarify here. '\n' and endl are not a compiler issue. They are very firmly defined by the standard. endl is defined to insert a newline and then flush the buffer. Period.

'\n' is OS/platform specific. If a compiler supports a platform, it supports the right newline conversion for that platform.
Nov 19 '07 #7
Banfa
9,065 Expert Mod 8TB
while(a=b)
{
execute code here
}

you will end with with an endless loop as a=b will always evaluate to being true (unless for some reason the assignment failed.
No you will end with an endless loop unless then value of b happens to be 0. The expression a=b assigns b to a and returns the new value of a. If b is 0 then 0 is assigned to a and 0 is returned.

In C++ the exception to this is exceptions, if a and/or b are objects and in evaluating the object and assignment an exception is raise the loop will also stop.
Nov 19 '07 #8
Banfa
9,065 Expert Mod 8TB
'\n' is OS/platform specific. If a compiler supports a platform, it supports the right newline conversion for that platform.
I thought this was only true for a stream/file opened in text mode.

For a stream/file opened in binary mode no conversation is done the code writes exactly what it is given so '/n' is written as byte value of 10 (assuming an ASCII execution character set).
Nov 19 '07 #9
oler1s
671 Expert 512MB
I thought this was only true for a stream/file opened in text mode.
Very true. I should have qualified my statement.
Nov 19 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Victor Irzak | last post by:
Hello, I have a g++-3.3.1 compiler (latest). I can't figure the type of endl, ends, flush. Here is my program. It compiles with VS.NET, Intel C++ compiler, but not with g++. If you can...
12
by: Michael Mellor | last post by:
I see so many posts on this newsgroup where people use std::endl but I can't see why or where they are getting it from. std::endl outputs a new line and flushes the stream's buffer. It is even 15.7...
9
by: dover | last post by:
For the code, outputfile << *p << endl; Someone suggests: Don't use endl here; it is flushing output stream every time. Use plain '\n'. What's the difference of using "endl" and "\n" above?...
4
by: ricardw | last post by:
Hi! I've tried to find an answer to this in the FAQ and elsewhere on the net, without success. I'm fairly sure that the issue must have turned up before and that at least a handful of people may...
8
by: Leo jay | last post by:
dear all, i want to write log on disk file when macro NEED_DO_LOG is defined. otherwise, do nothing. so i wrote code like this: #ifdef NEED_DO_LOG fstream fsLogFile (...); #else CDummyStream...
8
by: schwehr | last post by:
Hi All, I am trying to creat a class that that acts much like cerr and am having trouble figuring out if it is possible to use std::end with my class to designate then end of a group of items. ...
1
by: Steve Wonderful | last post by:
I was compiling a VC++ application in VS.NET 2003 and got the following error: The following line #1 will not complied and created the following MS compiler error. If I change endl to "\n" s in...
16
by: Jdban101 | last post by:
I am working on my second program ever, so bear with me. When I try to compile it, I get two error messages(sort of 3): In function `int main()': line 6: `endl' undeclared (first use this...
9
by: andrew.smith.cpp | last post by:
hi, whts the difference between the std::endl or "\n" ? because both do the same work Thanks
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.