为什么C / C ++字符串文字声明是单行的? [英] Why must C/C++ string literal declarations be single-line?

查看:207
本文介绍了为什么C / C ++字符串文字声明是单行的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么特别的原因在C ++中不允许多行字符串文字如下所示?

Is there any particular reason that multi-line string literals such as the following are not permitted in C++?

string script =
"
      Some
   Formatted
 String Literal
";

我知道多行字符串文字可以通过在每个换行符之前放一个反斜杠来创建。
我正在编写一个编程语言(类似于C),并希望允许轻松创建多行字符串(如上例所示)。

I know that multi-line string literals may be created by putting a backslash before each newline. I am writing a programming language (similar to C) and would like to allow the easy creation of multi-line strings (as in the above example).

有没有任何技术原因,避免这种字符串字面量?否则,我必须使用类似python的字符串文字与三重引号(我不想这样做):

Is there any technical reason for avoiding this kind of string literal? Otherwise I would have to use a python-like string literal with a triple quote (which I don't want to do):

string script =
"""
      Some
   Formatted
 String Literal
""";

为什么C / C ++字符串文字声明是单行的? / p>

Why must C/C++ string literal declarations be single-line?

推荐答案

人们必须考虑C不是一个应用程序编程语言,而是一个系统编程语言。它不会不准确,说它是专门设计重写Unix。考虑到这一点,没有EMACS或VIM,您的用户界面是串行终端。多行字符串声明在没有多行文本编辑器的系统上似乎有点无意义。 Furthmore字符串操作不会是一个主要关注的人在某个时间点写一个操作系统。传统的UNIX脚本工具集,如AWK和SED(在MANY其他人之间)是一个证明,他们没有使用C做重大的字符串操作。

One has to consider that C was not written to be a "Applications" programming language but a systems programming language. It would not be inaccurate to say it was designed expressly to rewrite Unix. With that in mind, there was no EMACS or VIM and your user interfaces were serial terminals. Multiline string declarations would seem a bit pointless on a system that did not have a multiline text editor. Furthmore string manipulation would not be a primary concern for someone looking write an OS at that particular point in time. The traditional set of UNIX scripting tools such as AWK and SED (amongst MANY others) are a testiment to the fact they weren't using C to do significant string manipulation.

额外的考虑,在70年代初(当C写时)并不罕见,提交您的程序在PUNCH卡和回来第二天得到他们。它会消耗额外的处理时间来编译一个程序与多行字符串文字?实际上它不是真的可以减少编译器的工作。但在大多数情况下,你会在第二天回来。但是没有人在填写一张穿孔卡,会把大量不需要的文字放入程序中。

Additional considerations, it was not uncommon in the early 70s (when C was written) to submit your programs on PUNCH CARDS and comeback the next day to get them. Would it have eaten up extra processing time to compile a program with multiline strings literals? Not really it can actually be less work for the compiler. But you were going to comeback for it the next day anyhow in most cases. But nobody who was filling out a punch card was going to put large amounts of text that wasn't needed in there programs.

在现代化的环境中,原因不包括除了设计者的偏好之外的多行字符串文字。从语法上讲,它可能更简单,因为你不必在分析字符串字面时考虑换行。

In a modern environment, there is probably no reason not to include multiline string literals other than designer's preference. Gramatically speaking it's probably simpler because you don't have to take linefeeds into consideration when parsing the string literal.

这篇关于为什么C / C ++字符串文字声明是单行的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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