C语言 C语言中的换行符是什么:\r或\n?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18379848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:13:23  来源:igfitidea点击:

What is the newline character in the C language: \r or \n?

cnewline

提问by andy

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?

C中的换行符是什么?我知道不同的操作系统有不同的行结束符,但它们会被转换为 C 换行符。那是什么性格?

回答by Crowman

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.

它是\n。当您读取或写入文本模式文件,或到 stdin/stdout 等时,您必须使用\n,C 将为您处理翻译。当您处理二进制文件时,根据定义,您是靠自己的。

回答by chux - Reinstate Monica

What is the newline character in the C language: \r or \n?

C语言中的换行符是什么:\r或\n?

The new-line may be thought of a somecharand it has the value of '\n'. C11 5.2.1

换行符可能被认为是somechar并且它的值为'\n'。C11 5.2.1

This C new-line comes up in 3 places: C source code, as a single charand as an end-of-line in file I/O when in textmode.

这个 C 换行符出现在 3 个地方:C 源代码,char文本模式下作为文件 I/O 中的单个和行尾。

  1. 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.

  2. In source code, the 2 characters \and nrepresent the charnew-line as \n. If ASCII is used, this charwould have the value of 10.

  3. 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.

  1. 许多编译器将源文本视为 ASCII。在这种情况下,代码 10,有时是 13,有时是成对的 13,10 作为源代码的换行符。如果源代码在另一个字符集中,可能会使用不同的代码。这个换行符通常标志着一行源代码(这里实际上有点复杂)、// 注释和 # 指令的结束。

  2. 在源代码中,2 个字符\n表示char换行符为\n. 如果使用 ASCII,char则其值为 10。

  3. 在文件 I/O 中,在文本模式下,在读取输入文件(和标准输入)的字节时,取决于环境,当字节值为 10 (Unix), 13,10, (*1) (Windows)、13 (Old Mac??) 和其他变体被转换为“\n”。写入文件(或标准输出)时,会发生反向转换。
    注意:二进制模式下的文件 I/O 不进行转换。

The '\r'in source code is the carriage return char.

'\r'源代码是回车char

(*1) A lone 13and/or 10may also translate into \n.

(*1) 单独的13和/或10也可以翻译成\n

回答by pavel.lazar

If you mean by newline the newline characterit is \nand \ris 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")

如果换行是换行符,它是\n\r载体返回字符,但如果换行是行结束,那么它取决于操作系统:DOS 使用回车和换行符 ( "\r\ n") 作为行结尾,Unix 仅使用换行符 ( "\n")

回答by TittleTattle

'\r' = carriage return and '\n' = line feed.

'\r' = 回车,'\n' = 换行。

In fact, there are some different behaviors when you use them in different OSes. On Unix it is '\n', but it is '\r''\n' on Windows.

事实上,当你在不同的操作系统中使用它们时,会有一些不同的行为。在 Unix 上它是 '\n',但在 Windows 上它是 '\r''\n'。