\ n是C中的多字符吗? [英] Is \n multi-character in C?

查看:209
本文介绍了\ n是C中的多字符吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到\ n由CR&如果.每个都有自己的ASCII码.

I read that \n consists of CR & LF. Each has their own ASCII codes.

那么C中的\ n是用单个字符表示还是多个字符?

So is the \n in C represented by a single character or is it multi-character?

请指定您的答案,而不是简单地说是,是" 不是,不是"

Kindly specify your answer, rather than simply saying "yes, it is" or "no, it isn't"

推荐答案

通常:'\n'是单个字符,表示换行符. '\r'是单个字符,表示回车.它们是它们自己的独立ASCII字符.

Generally: '\n' is a single character, which represents a newline. '\r' is a single character, which represents a carriage-return. They are their own independent ASCII characters.

之所以会出现问题,是因为在实际的文件表示中,基于UNIX的系统倾向于单独使用'\n'来表示您在键盘上敲击"enter"或"return"时的想法,而Windows使用的是'\r'直接跟一个'\n'.

Issues arise because in the actual file representation, UNIX-based systems tend to use '\n' alone to represent what you think of when you hit "enter" or "return" on the keyboard, whereas Windows uses a '\r' followed directly by a '\n'.

在文件中:

"This is my UNIX file\nwhich spans two lines"
"This is my Windows file\r\nwhich spans two lines"

当然,像所有二进制数据一样,这些字符都与解释有关,并且该解释取决于使用数据的应用程序. 制作C字符串时,请粘贴到'\ n',除非您想要字面意义上的回车,因为正如人们在评论中指出的那样,OS表示形式与您无关.包括C在内的IO库应该可以自行处理并将其抽象出来.

Of course, like all binary data, these characters are all about interpretation, and that interpretation depends on the application using the data. Stick to '\n' when you are making C-strings, unless you want a literal carriage-return, because as people have pointed out in the comments, the OS representation doesn't concern you. IO libraries, including C's, are supposed to handle this themselves and abstract it away from you.

出于好奇,以十进制表示,ASCII中的'\n'为10,'\r'为13,但是请注意,这是ASCII标准,而不是C标准.

For your curiosity, in decimal, '\n' in ASCII is 10, '\r' is 13, but note that this is the ASCII standard, not a C standard.

这篇关于\ n是C中的多字符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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