Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 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?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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.

share|improve this answer

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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