Difference between '\n' and '\r\n' ...

Discussion in 'C' started by Bhullarz, Dec 29, 2007.

  1. Bhullarz

    Bhullarz New Member

    Joined:
    Nov 15, 2006
    Messages:
    253
    Likes Received:
    13
    Trophy Points:
    0
    Occupation:
    System Manager
    Home Page:
    http://www.tutors161.com
    I recently faced a problem regarding these 2 Escape Sequences. So, I thought I should share the difference between these two to clarify to all who may hay have confusion like me.

    Thanks to Shabbir too......

    The Difference



    There are a few characters which can indicate a new line. The usual ones are these two:

    * '\n' or '0x0A' (10 in decimal) -> This character is called "Line Feed" (LF).
    * '\r' or '0x0D' (13 in decimal) -> This one is called "Carriage return" (CR).​
    Different Operating Systems handle newlines in a different way. Here is a short list of the most common ones:

    * DOS and Windows​
    They expect a newline to be the combination of two characters, namely '\r\n' (or 13 followed by 10).

    * Unix (and hence Linux as well)​
    Unix uses a single '\n' to indicate a new line.

    * Mac​
    Macs use a single '\r'.

    This difference gives rise to a number of problems. For example, a file created under Unix (so with newlines as a single LF) will not open correctly under Window's Notepad. Any Windows program that expects newlines to be CRLF will not work correctly with these files.

    To unify things a bit, so that writing portable C/C++ programs is possible, file streams have both a "translated" and an "untranslated" mode. If you open a file in translated mode, the runtime library will convert a '\n' to the appropriate newline character(s). If the following program is compiled under Unix, the file will contain a single LF to indicate the newline. If it's compiled under windows, it will contain a CRLF.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        FILE *fp = fopen("testfile.txt", "w"); 
        fprintf(fp, "Hello World\n"); 
        fclose(fp); 
        return 0; 
    }
    
    If you look at the generated file with a hex editor, you will see that the windows version has generated the following:
    Code:
    H    e    l    l    o         W    o    r    l    d   CR   LF
    0x48 0x65 0x6C 0x6C 0x6F 0x20 0x57 0x6F 0x72 0x6C 0x64 0x0D 0x0A
    
    So file streams are handled in a transparent way, provided of course that you only handle files compatible with your operating system. But many times you have to pass multi-line strings directly to some system functions.

    In practice

    In Windows you have to pass multi-line strings with '\r\n', otherwise the system functions don't recognize them correctly as multi-line. This is true e.g. for setting the text of Edit controls, Labels, Windows etc. Also, when you read multi-line text from a file that initially contains '\r\n' in translated mode, the string in memory will contain only a single '\n'. See for example the documentation on MSDN about 'fread()':
    If you want to be able to read text files written on different operating systems, you have to open the file in binary (= untranslated) mode and check for the different newlines yourself.
     
    Last edited by a moderator: Dec 29, 2007
    shabbir and ks.ravisankar like this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Re: Difference between '\n' and '\r\n ...

    Nicely explained.
     
  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Nice article. You can use EditPlus opens all types of files, it automatically detects line endings!
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Thats when using the editor but you should also know which OS does what newline characters
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  6. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Very nice !!! Thanks for this kind of info!!!
     
  7. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice