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

查看:256
本文介绍了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).

有任何技术原因,以避免这种字符串的?否则,我将不得不使用一个蟒蛇般的字符串和三重引号(我不想做):

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 ++字符串声明是单行?

推荐答案

人们必须考虑下不写是一个应用程序的编程语言,但一个系统编程语言。它不会是不准确的说,它被设计的前pressly重写Unix的。考虑到这一点,没有Emacs或VIM和你的用户界面是串行终端。多行字符串的声明看起来,没有一个多行文字编辑器的系统上有点毫无意义。 Furthmore字符串操作就不会有人看在那个时间点特别写一个操作系统首要关注的问题。传统的一套UNIX脚本工具,如AWK和SED(其中包括许多其他的)是一个testiment他们没有使用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年代初时(下写)罕见的打孔卡和复出的第二天,提交您的方案,以得到他们。难道吃了额外的处理时间来编译多行字符串常量的程序?没有真正它实际上是编译器更少的工作。但是,你在大多数情况下,要复出了它第二天无论如何。但是,没有人谁是填写一个打卡打算把大量的,这不是在那里程序所需的文字。

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.

在现代环境中,有可能是没有理由不包括除设计师的preference其他多行字符串。 Gramatically来说它可能是简单,因为你不必解析字符串时考虑换行考虑。

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天全站免登陆