What is the difference between '\0' and '\n' in the C programming language?

16,480

Solution 1

'\0' is a NULL character (ASCII 0), which is actually a string terminator too... (C strings are NULL-terminated): if you have a string like "this is\0a string", the part after the '\0' will be ignored (even if it will actually be inside the generated code).

'\n' is a newline (ASCII 10). It is noteworthy that in some circumstances, this newline character can actually be transformed. For example, on Windows, where the newline in files is indicated by the "\r\n" sequence (two bytes: ASCII 13, carriage return, followed by ASCII 10, line feed), if you write to a file (e.g. using fprintf()) a string containing a '\n' character, it will be automatically converted to a "\r\n" sequence if the file is open in ASCII mode (which is generally the default).

Solution 2

'\0' is a null: this terminates a string. '\n' is a newline

Solution 3

'\0' is a NULL character, which indicates the end of a string in C. (printf("%s") will stop printing at the first occurence of \0 in the string.

'\n' is a newline, which will simply make the text continue on the next line when printing.

Solution 4

\0 is the null byte, used to terminate strings. \n is the newline character, 10 in ASCII, used (on Unix) to separate lines.

Solution 5

'\0' is a character constant that is written as octal-escape-sequence. Its value is 0. It is not the same as '0'. The last has value 48 in ASCII or 240 in EBCDIC

'\n' is a character constant that is written as simple-escape-sequence and denotes the new line character. Its value is equal to 10.

Share:
16,480
Daniel Gartmann
Author by

Daniel Gartmann

I am an IT-Security expert with a software development background that helps companies innovate and meet their strategic goals by turning security into an enabler and not a blocker.

Updated on June 05, 2022

Comments

  • Daniel Gartmann
    Daniel Gartmann almost 2 years

    What is the difference between the '\0' character and the '\n' character in the C programming language?

  • Ale
    Ale over 9 years
    @alvits It can't be CRLF, because '\n' is a character. However, it is true that the I/O routines (e.g. fprintf()) will indeed convert it to "\r\n" (CRLF), but not if the file was opened in binary mode. It is a good point, I'll edit to mention it.
  • M.M
    M.M over 9 years
    @alvits '\n' in C code is always newline. You're talking about translations performed when reading or writing text streams. (i.e. what's in the file on your disk may not match what you see in your code when reading).
  • alvits
    alvits over 9 years
    @MattMcNabb - yes you are both absolutely correct. I should have read the answer completely then leave comment accurately. I'm deleting my inaccurate comment to prevent confusion to visitors of this site.
  • Ale
    Ale over 9 years
    @alvits Actually your comment was useful in the sense that it helped improving the answer with the details concerning the \n to \r\n conversion, which is something important to know.
  • PatrickSteiner
    PatrickSteiner over 6 years
    Does that mean that a string like "This \0 is a \0 string." would be terminated after "This " and the second \0 would be ignored?
  • Ale
    Ale over 6 years
    When you compile the program, the whole thing will be in the executable file and in memory. But any "normal" routine expecting a C string (i.e. NULL terminated) will only "see" the string until the first \0, i.e., strlen() will return 5 (i.e. length of "This "). But if you just look into the memory, i.e. using pointers/array indexes, the whole string will be there, including the intermediate \0.