As far as I can tell, before C++11, string literals were handled in almost exactly the same way between C and C++.
Now, I acknowledge that there are differences between C and C++ in the handling of wide string literals.
The only differences that I have been able to find are in the initialization of an array by string literal.
char str[3] = "abc"; /* OK in C but not in C++ */
char str[4] = "abc"; /* OK in C and in C++. Terminating zero at str[3] */
And a technical difference that only matters in C++. In C++ "abc"
is const char [4]
while in C it is char [4]
. However, C++ has a special rule that allows the conversion to const char *
and then to char *
to retain C compatibility up until C++11 when that special rule is no longer applied.
And a difference in allowed lengths of literals. However, as a practical matter any compiler that compiles both C and C++ code will not enforce the lower C limit.
I have some interesting links that apply:
Are there any other differences?
Raw strings
A noticeable difference is that C++'s string literals are a superset of C ones. Specifically C++ now supports raw strings (not supported in C), technically defined at §2.14.15 and generally used in HTML and XML where
"
is often encountered.Raw strings allow you to specify your own delimiter (up to 16 characters) in the form:
This is particularly useful to avoid unnecessary escaping characters by providing your own string delimiter. The following two examples show how you can avoid escaping of
"
and(
respectively:Live demo
char
vsconst char
Another difference, pointed out in the comments, is that string literals have type
char [n]
in C, as specified at §6.4.5/6:while in C++ they have type
const char [n]
, as defined in §2.14.5/8:This doesn't change the fact that in both standard (at §6.4.5/7 and 2.14.5/13 for C and C++ respectively) attempting to modify a string literal results in undefined behavior.
Unspecified vs Implementation defined (ref)
Another subtle difference is that in C, wether the character arrays of string literals are different is unspecified, as per §6.4.5/7:
while in C++ this is implementation defined, as per §2.14.5/13:
The best way to answer your question is to rewrite it as a Program that compiles identically when using a "C" or "C++" Compiler, I will assume you are using GCC but other (correctly written) Compiler Toolchains should provide similar results.
First I will address each point you posed then I will give a Program that provides the answer (and Proof).
They still can be handled the same way using various Command Line Parameters, in this example I will use "-fpermissive" (a Cheat). You are better off finding out why you are getting Warnings and writing NEW Code to avoid ANY Warning; only use CLP 'cheats' to compile OLD Code.
Write new Code correctly (no cheats and no Warnings, that there be no Errors goes without saying).
There does not have to be (many differences) since you can cheat most or all of them away depending on the circumstances. Cheating is wrong, learn to program correctly and follow modern Standards not the mistakes (or awkwardness) of the past. Things are done a certain way to be helpful both to you, and to the Compiler in some cases (remember YOU are not the only one who 'sees' your Code).
In this case the Compiler wants enough space allocated to terminate the String with a '0' (zero byte). That permits the use of a print (and some other) Function without specifying the length of the String.
IF you are simply trying to compile an existing Program you obtained from somewhere and do not want to re-write it, you simply want to compile it and run it, then use the cheats (if you must) to get past the Warnings and force the compilation to an executable.
No.
.
See this example Program. I slightly modified your question to make it into a Program. The result of compiling this Program with a "C" or C++" Compiler is identical.
Copy-and-Paste the example Program text below to a File called "test.c", then follow the instructions at the start. Simply 'cat' the File so you can backscroll it (and see it) without opening a Text Editor, then Copy-and-Paste each Line beginning with the Compiler Commands (the next three).
Note, that as pointed out in the Comments, that running this Line "g++ -S -o test_c++.s test.c" produces an Error (using a modern g++ Compiler) since the container is not long enough to hold the String.
You should be able to read this Program and not actually need to compile it to see the Answer but it will compile and produce the Output for you to examine should you desire to do so.
As you can see the Varable "str1" is not long enough to hold the String when it is null terminated, that produces an Error on a modern (and correctly written) g++ Compiler.