C++ – Concatenating Adjacent String Literals

clanguage-features

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?

Best Answer

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.