\n and endl: What's the difference?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lotus18
    Contributor
    • Nov 2007
    • 865

    \n and endl: What's the difference?

    Hello I'm a newbie in c++

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

    1. \n and endl?
    2. = and ==?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    1) Depends on your compiler, I imagine.

    2) = is assignment, == is comparison.

    Comment

    • lotus18
      Contributor
      • Nov 2007
      • 865

      #3
      Originally posted by Ganon11
      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 : )

      Comment

      • gpraghuram
        Recognized Expert Top Contributor
        • Mar 2007
        • 1275

        #4
        Originally posted by lotus18
        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...

        Comment

        • coffear
          New Member
          • Nov 2007
          • 20

          #5
          Originally posted by lotus18
          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.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            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.

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              ‘\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.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Originally posted by coffear
                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.

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  Originally posted by oler1s
                  '\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).

                  Comment

                  • oler1s
                    Recognized Expert Contributor
                    • Aug 2007
                    • 671

                    #10
                    Originally posted by Banfa
                    I thought this was only true for a stream/file opened in text mode.
                    Very true. I should have qualified my statement.

                    Comment

                    Working...