String Literal Concatenation

When the ‘C’ or C++ parser reads a source file, it concatenates any string literals it finds that are separated only by whitespace. That means that you may break up strings, even over several lines, to make code more readable, or even to mimic its appearance on the screen.

For example, you can write, e.g.

printf (
    "First line" "n"
    "t" "Second line" "n"
    "t" "Third line" "n"
    "t" "Fourth line" "n"
    );

separating the control characters, and setting the text out somewhat similarly to how it will appear on the screen, rather than the less-readable

printf ("First linentSecond linentThird linentFourth linen");

As far as the compiler is concerned, these are identical.

Similarly, you can write, e.g.

cout << "t" """ "One quoted line" """ << endl
        << "t" """ "Another quoted line" """ << endl
        << endl;

(again just separating the control and escape character strings) rather than

cout << "t"One quoted line"" << endl
        << "t"Another quoted line"" << endl << endl;

I find the former more readable. This may just be a matter of taste.

Note that whitespace outside the quotes is not part of the string. Thus “the” “rapist” is the same as “therapist”.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: