c和java语言中的换行符 [英] New line character in c and java language

查看:63
本文介绍了c和java语言中的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在行分隔符取决于系统,但在 c 程序中我使用 '\n' 作为行分隔符,无论我在 windows 还是 linux 中运行它都可以正常工作.为什么 ??

Now line separator is system dependent but while in a c program i use '\n' for line separator , it works fine whether i run it in windows or linux . why ??

在 java 中我们必须使用 %n,因为它依赖于系统,那么我们为什么在 c 中使用 '\n' 作为新行而不管我们运行它的操作系统是什么?

In java we have to use %n as it is system dependent then how come we use '\n' in c for new line irrespective of os in which we run it ?

推荐答案

C99标准不得不说:

5.2.2 Character display semantics
[...]
2. Alphabetic escape sequences representing nongraphic characters in the execution
character set are intended to produce actions on display devices as follows:
[...]
\n (new line) Moves the active position to the initial position of the next line.

这意味着如果您打印\n",则必须在 Windows 上将其更改为\r\n"才能满足此要求.但这只是为显示设备定义的,而不是文件.没有关于如何在 text-file 中表示这个(或任何其他空格)字符的规范,但是:

This means that if you print "\n", it has to be changed to "\r\n" on windows in order to fulfill this requirement. But this is only defined this way for display devices, not for files. There is no specification on how this (or any other whitespace) character should be represented in text-file, however:

5.2.2 Character display semantics
[...]
3. Each of these escape sequences shall produce a unique implementation-defined value
which can be stored in a single char object. The external representations in a text file
need not be identical to the internal representations, and are outside the scope of this
International Standard.

因此,对您的问题的直接回答是 - 它按您的预期工作,因为 C 标准在将 \n 字符打印到显示设备或存储在文本文件.但是,它确实指定了打印时的行为方式 - 它应该移动到下一行的开头.

So the direct answer to your question is - it works as you would expect because the C standard does not say anything about binary representation of \n character when it's printed to the display device or stored in text-file. It does, however, precisely specify how it should behave when printed - it should move to the beginning of next line.

这篇关于c和java语言中的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆