21

C and C++ compiles adjacent string literals as a single string literal. For example this:

"Some text..." "and more text"

is equivalent to:

"Some text...and more text"

In other C-family languages like C# or Java, this is a syntax error (which is perfectly fine BTW).

What is the rationale/historical reason for C and C++ to do this?

2 Answers 2

28

The original C language was designed in 1969-1972 when computing was still dominated by the 80 column punched card. Its designers used 80 column devices such as the ASR-33 Teletype. These devices did not automatically wrap text, so there was a real incentive to keep source code within 80 columns. Fortran and Cobol had explicit continuation mechanisms to do so, before they finally moved to free format.

It was a stroke of brilliance for Dennis Ritchie (I assume) to realise that there was no ambiguity in the grammar and that long ASCII strings could be made to fit into 80 columns by the simple expedient of getting the compiler to concatenate adjacent literal strings. Countless C programmers were grateful for that small feature.

Once the feature is in, why would it ever be removed? It causes no grief and is frequently handy. I for one wish more languages had it. The modern trend is to have extended strings with triple quotes or other symbols, but the simplicity of this feature in C has never been outdone.

2
  • 8
    Another reason is that it allows concatenation of preprocessor macros defined as string literals, e.g., #define FOO "foo-value" followed later by "FOO's value is " FOO "."
    – Blrfl
    Sep 4, 2015 at 10:55
  • 3
    @Blrfl: Just so. It's important to realise that string concatenation takes place after macro substitution is complete.
    – david.pfx
    Sep 5, 2015 at 0:26
7

C has no specific string concatenation operator (+) like C# and Java. In C# or Java, when the compiler sees

"a" + "b"

it can compile the code exactly as if

"ab"

were written in the source code. In C, however, there is no similarly easy syntax for describing concatenation of strings that the compiler can recognise and pre-calculate. So the designers of C decades ago chose that

"a" "b"

would mean exactly the same thing as

"ab"

Naturally C++ inherited the same convention. While the standard C++ library overloads + on std::string to mean string concatenation, the compiler does not attempt to coalesce "a" + "b" because that is actually an error (you can't add two const char * pointers together).

1
  • 1
    C also has no specific string type, opting instead for pointers to characters in memory. You can't add pointers, and even if + were somehow made to mean concatenation, you still have to solve the problem of where in memory the concatenated string goes.
    – Blrfl
    Sep 4, 2015 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.