What is the newline character in C? I know that different OS have different line-ending characters, but they get translated into the C newline character. What is that character?
问题:
回答1:
It's \n
. When you're reading or writing text mode files, or to stdin/stdout etc, you must use \n
, and C will handle the translation for you. When you're dealing with binary files, by definition you are on your own.
回答2:
The new-line may be thought of a some char
and it has the value of '\n'
. C11 5.2.1
This C new-line comes up in 3 places: C source code, as a single char
and as an end-of-line in file I/O when in text mode.
Many compilers will treat source text as ASCII. In that case, codes 10, sometimes 13, and sometimes paired 13,10 as new-line for source code. Had the source code been in another character set, different codes may be used. This new-line typically marks the end of a line of source code (actually a bit more complicated here), // comment, and # directives.
In source code, the 2 characters text
\n
represent thechar
new-line. If ASCII is used, thischar
would have the value of 10.In file I/O, in text mode, upon reading the bytes of the input file (and stdin), depending on the environment, when bytes with the value(s) of 10 (Unix), 13,10, (*1) (Windows), 13 (Old Mac??) and other variations are translated in to a '\n'. Upon writing a file (or stdout), the reverse translation occurs.
Note: File I/O in binary mode makes no translation.
The '\r'
in source code is the carriage return char
.
(*1) A lone 13 and/or 10 may also translate into \n
.
回答3:
If you mean by newline the newline character it is \n and \r is the carrier return character, but if you mean by newline the line ending then it depends on the operating system: DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n")
回答4:
'\r' = carriage return , '\n' = line feed , In fact there are some different behaviors when you use them in different OS.In Unix it is '\n' but it is '\r''\n' in Windows.I hope it can help you.