Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
String literal
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Motivation=== In C, where the concept and term originate, string literal concatenation was introduced for two reasons:<ref>{{Cite book | isbn = 0-929306-07-4 | title = Rationale for the ANSI C Programming Language | year = 1990 | publisher = Silicon Press | url = http://www.lysator.liu.se/c/rat/title.html |page=[https://books.google.com/books?id=yxLISD0TAbEC&q=%22String%20literal%20concatenation%22&pg=PA31 31] }}, [http://www.lysator.liu.se/c/rat/c1.html#3-1-4 3.1.4 String literals]: "A long string can be continued across multiple lines by using the backslash-newline line continuation, but this practice requires that the continuation of the string start in the first position of the next line. To permit more flexible layout, and to solve some preprocessing problems (see Β§3.8.3), the Committee introduced string literal concatenation. Two string literals in a row are pasted together (with no null character in the middle) to make one combined string literal. This addition to the C language allows a programmer to extend a string literal beyond the end of a physical line without having to use the backslash-newline mechanism and thereby destroying the indentation scheme of the program. An explicit concatenation operator was not introduced because the concatenation is a lexical construct rather than a run-time operation."</ref> * To allow long strings to span multiple lines with proper indentation in contrast to line continuation, which destroys the indentation scheme; and * To allow the construction of string literals by macros (via [[stringizing]]).<ref>{{Cite book | isbn = 0-929306-07-4 | title = Rationale for the ANSI C Programming Language | year = 1990 | publisher = Silicon Press | url = http://www.lysator.liu.se/c/rat/title.html |page=[https://books.google.com/books?id=yxLISD0TAbEC&q=%22String%20literal%20concatenation%22&pg=PA65 6566] }}, [http://www.lysator.liu.se/c/rat/c8.html#3-8-3-2 3.8.3.2 The # operator]: "The # operator has been introduced for stringizing. It may only be used in a #define expansion. It causes the formal parameter name following to be replaced by a string literal formed by stringizing the actual argument token sequence. In conjunction with string literal concatenation (see Β§3.1.4), use of this operator permits the construction of strings as effectively as by identifier replacement within a string. An example in the Standard illustrates this feature."</ref> In practical terms, this allows string concatenation in early phases of compilation ("translation", specifically as part of lexical analysis), without requiring phrase analysis or constant folding. For example, the following are valid C/C++: <syntaxhighlight lang="c"> char *s = "hello, " "world"; printf("hello, " "world"); </syntaxhighlight> However, the following are invalid: <syntaxhighlight lang="c"> char *s = "hello, " + "world"; printf("hello, " + "world"); </syntaxhighlight> This is because string literals have [[Array data type|array type]], <code>char [''n'']</code> (C) or <code>const char [''n'']</code> (C++), which cannot be added; this is not a restriction in most other languages. This is particularly important when used in combination with the [[C preprocessor]], to allow strings to be computed following preprocessing, particularly in macros.<ref name=guidopythonideas/> As a simple example: <syntaxhighlight lang="c"> char *file_and_message = __FILE__ ": message"; </syntaxhighlight> will (if the file is called a.c) expand to: <syntaxhighlight lang="c"> char *file_and_message = "a.c" ": message"; </syntaxhighlight> which is then concatenated, being equivalent to: <syntaxhighlight lang="c"> char *file_and_message = "a.c: message"; </syntaxhighlight> A common use case is in constructing printf or scanf [[format string]]s, where format specifiers are given by macros.<ref>''C/C++ Users Journal,'' Volume 19, [https://books.google.com/books?id=gGpVAAAAMAAJ&q=%22string+literal+concatenation%22 p. 50]</ref><ref>{{cite web|url=https://stackoverflow.com/questions/2504536/why-allow-concatenation-of-string-literals |title=python - Why allow concatenation of string literals? |publisher=Stack Overflow |access-date=2016-06-22}}</ref> A more complex example uses [https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html stringification] of integers (by the preprocessor) to define a macro that expands to a sequence of string literals, which are then concatenated to a single string literal with the file name and line number:<ref>{{cite web|url=http://www.decompile.com/cpp/faq/file_and_line_error_string.htm |title=LINE__ to string (stringify) using preprocessor directives |website=Decompile.com |date=2006-10-12 |access-date=2016-06-22}}</ref> <syntaxhighlight lang="c"> #define STRINGIFY(x) #x #define TOSTRING(x) STRINGIFY(x) #define AT __FILE__ ":" TOSTRING(__LINE__) </syntaxhighlight> Beyond syntactic requirements of C/C++, implicit concatenation is a form of [[syntactic sugar]], making it simpler to split string literals across several lines, avoiding the need for line continuation (via backslashes) and allowing one to add comments to parts of strings. For example, in Python, one can comment a [[regular expression]] in this way:<ref>The Python Language Reference, 2. Lexical analysis, [https://docs.python.org/2/reference/lexical_analysis.html#string-literal-concatenation 2.4.2. String literal concatenation]: "This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings, for example:</ref> <syntaxhighlight lang="python"> re.compile("[A-Za-z_]" # letter or underscore "[A-Za-z0-9_]*" # letter, digit or underscore ) </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)