Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
";

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
""";

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

like image 844
Rizo Avatar asked Jun 22 '10 13:06

Rizo


People also ask

Why are C string literals read-only?

The string literal is stored in the read-only part of memory by most of the compilers. The C and C++ standards say that string literals have static storage duration, any attempt at modifying them gives undefined behavior. s is just a pointer and like any other pointer stores address of string literal.

What is special about a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

What is the difference between string and string literal in C?

C-strings are simply implemented as a char array which is terminated by a null character (aka 0 ). This last part of the definition is important: all C-strings are char arrays, but not all char arrays are c-strings. C-strings of this form are called “string literals“: const char * str = "This is a string literal.

How do you declare a string literal?

The best way to declare a string literal in your code is to use array notation, like this: char string[] = "I am some sort of interesting string. \n"; This type of declaration is 100 percent okey-doke.


2 Answers

The terse answer is "because the grammar prohibits multiline string literals." I don't know whether there is a good reason for this other than historical reasons.

There are, of course, ways around this. You can use line splicing:

const char* script = "\
      Some\n\
   Formatted\n\
 String Literal\n\
";

If the \ appears as the last character on the line, the newline will be removed during preprocessing.

Or, you can use string literal concatenation:

const char* script = 
"      Some\n"
"   Formatted\n"
" String Literal\n";

Adjacent string literals are concatenated during preprocessing, so these will end up as a single string literal at compile-time.

Using either technique, the string literal ends up as if it were written:

const char* script = "      Some\n   Formatted\n  String Literal\n";
like image 50
James McNellis Avatar answered Sep 23 '22 17:09

James McNellis


One has to consider that C was not written to be an "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. Furthermore, string manipulation would not be a primary concern for someone looking to 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 testament to the fact they weren't using C to do significant string manipulation.

Additional considerations: it was not uncommon in the early 70s (when C was written) to submit your programs on PUNCH CARDS and come back 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 come back 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 their programs.

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

like image 36
NoMoreZealots Avatar answered Sep 23 '22 17:09

NoMoreZealots