Const char * initialization

This is one use that I found in open source software. And I don’t understand how it works. when I go out to stdout, it was "version 0.8.0".

const char version[] = " version " "0" "." "8" "." "0"; 
+8
c ++
source share
7 answers

This is the basic function of both C89 and C ++ 98, called "adjacent string concatenation" or near it.

Basically, if two string literals are adjacent to each other without punctuation between them, they are combined into one string, as your output shows.


In the C ++ 98 standard, section § 2.1 "Translation phases [lex.phases]":

6 Adjacent regular string literals are related to each other. Adjacent widescreen literal tokens are concatenated.

This is after the preprocessor completes.

In the C99 standard, the relevant section is §5.1.2.1 “Translation phases”, and it says:

6 Conjugate string literals are combined.

The wording will be very similar to any other C or C ++ standard you can rely on (and I really admit that C ++ 98 and C99 are replaced by C ++ 11 and C11, I just don't have electronic copies of the final standards, however )

+7
source share

It is called string concatenation - when you put two (or more) quotation marks next to each other in the source code without anything in between, the compiler combines them into one line. This is most often used for long lines - just a few lines:

 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 created, you had to do this with a rather clumsy line continuation, leaving a backslash at the end of each line (and making sure that this is the end, as most compilers will not consider this as a line continuation if after the backslash there were gaps). There was also an ugliness when he discarded padding, because any spaces at the beginning of subsequent lines could be included in the line.

This can cause a small problem if you intended to place a comma between the lines, for example, when initializing an array of pointers to char. If you miss a comma, the compiler will not warn you about this - you will simply get one line, which includes what was intended for two separate ones.

+9
source share

Part of the standard C ++ implementation claims that string literals that are next to each other will be merged 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 sequence specified by any sequence of the adjacent character and identically prefix literal line tokens are combined into one multibyte character sequence."

For C ++:

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

+6
source share
 const char version[] = " version " "0" "." "8" "." "0"; 

matches with:

 const char version[] = " version 0.8.0"; 

The compiler concatenates adjacent fragments of string literals, creating one large part of the string literal.

As a side element, const char* (which is in your name) is not the same as char char[] (which is in your published code).

+3
source share

The compiler automatically concatenates string literals written one after another (separated only by spaces). It's as if you wrote

 const char version[] = "version 0.8.0"; 

EDIT: fixed preprocessor for the compiler

+2
source share

Adjacent string literals concatenated :

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

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

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

cout <"Four points and seven years"
"back, our ancestors gave birth" "a new nation on this continent."

+2
source share

Simply placing the lines one after the other combines them at compile time, so:

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

This is a strange use of this function, as a rule, you must use the #define lines:

 #define FOO "World!" puts("Hello, " FOO); 

Will compile in the same way as:

 puts("Hello, World!"); 
0
source share

All Articles