简体   繁体   中英

const char* initialization

This is one usage I found in a open source software.And I don't understant how it works. when I ouput it to the stdout,it was "version 0.8.0".

const char version[] = " version " "0" "." "8" "." "0";

It's called string concatenation -- when you put two (or more) quoted strings next to each other in the source code with nothing between them, the compiler puts them together into a single string. This is most often used for long strings -- anything more than one line long:

char whatever[] = "this is the first line of the string\n"
    "this is the second line of the string\n"
    "This is the third line of the string";

Before string concatenation was invented, you had to do that with a rather clumsy line continuation, putting a backslash at the end of each line (and making sure it was the end, because most compilers wouldn't treat it as line continuation if there was any whitespace after the backslash). There was also ugliness with it throwing off indentation, because any whitespace at the beginning of subsequent lines might be included in the string.

This can cause a minor problem if you intended to put a comma between the strings, such as when initializing an array of pointers to char. If you miss a comma, the compiler won't warn you about it -- you'll just get one string that includes what was intended to be two separate ones.

This is a basic feature of both C89 and C++98 called 'adjacent string concatenation' or thereabouts.

Basically, if two string literals are adjacent to each other with no punctuation in between, they are merged into a single string, as your output shows.


In the C++98 standard, section §2.1 'Phases of translation [lex.phases]' says:

6 Adjacent ordinary string literal tokens are concatenated. Adjacent wide string literal tokens are concatenated.

This is after the preprocessor has completed.

In the C99 standard, the corresponding section is §5.1.2.1 'Translation Phases' and it says:

6 Adjacent string literal tokens are concatenated.

The wording would be very similar in any other C or C++ standard you can lay hands on (and I do recognize that both C++98 and C99 are superseded by C++11 and C11; I just don't have electronic copies of the final standards, yet).

Part of the C++ standard implementation states that string literals that are beside each other will be concatenated together.

Quotes from C and C++ Standard:

For C (quoting C99, but C11 has something similar in 6.4.5p5):

(C99, 6.4.5p5) "In translation phase 6, the multibyte character sequences specified by any sequence of adjacent character and identically-prefixed string literal tokens are concatenated into a single multibyte character sequence."

For C++:

(C++11, 2.14.5p13) "In translation phase 6 (2.2), adjacent string literals are concatenated."

const char version[] = " version " "0" "." "8" "." "0";

is same as:

const char version[] = " version 0.8.0";

Compiler concatenates the adjacent pieces of string-literals, making one bigger piece of string-literal.

As a sidenote, const char* (which is in your title) is not same as char char[] (which is in your posted code).

The compiler automatically concatenates string literals written after each other (separated by white-space only).. It is as if you have written

const char version[] = "version 0.8.0";

EDIT: corrected pre-processor to compiler

Adjacent string literals are concatenated :

When specifying string literals, adjacent strings are concatenated. Therefore, this declaration:

char szStr[] = "12" "34"; is identical to this declaration:

char szStr[] = "1234"; This concatenation of adjacent strings makes it easy to specify long strings across multiple lines:

cout << "Four score and seven years "
"ago, our forefathers brought forth "
"upon this continent a new nation.";

Simply putting strings one after the other concatenates them at compile time, so:

"Hello" ", " "World!" => "Hello, World!"

This is a strange usage of the feature, usually it is to allow #define strings to be used:

#define FOO "World!"

puts("Hello, " FOO);

Will compile to the same as:

puts("Hello, World!");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM