Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Literal Differences Between C and C++

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:

  • http://david.tribble.com/text/cdiffs.htm
  • http://www.coding-guidelines.com/cbook/c90c++.pdf

Are there any other differences?

like image 568
Zan Lynx Avatar asked Apr 18 '14 00:04

Zan Lynx


People also ask

What are string literals in C?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

What is the difference between string and string literal in C?

C-strings are simply implemented as a char array which is terminated by a null character (aka 0 ). This last part of the definition is important: all C-strings are char arrays, but not all char arrays are c-strings. C-strings of this form are called “string literals“: const char * str = "This is a string literal.

What is the difference between string and literal?

Definition. String literal in Java is a set of characters that is created by enclosing them inside a pair of double quotes. In contrast, String Object is a Java is a set of characters that is created using the new() operator. Thus, this explains the main difference between string literal and string object.

What is the difference between C and C++ string styles?

You can (if you need one) always construct a C string out of a std::string by using the c_str() method. Show activity on this post. C++ strings are much safer,easier,and they support different string manipulation functions like append,find,copy,concatenation etc.


1 Answers

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:

R"delimiter(char sequence)delimiter"

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:

std::cout << R"(a"b"c")";      // empty delimiter
std::cout << '\n';
std::cout << R"aa(a("b"))aa";  // aa delimiter
// a"b"c"
// a("b")

Live demo


char vs const 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:

For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.

while in C++ they have type const char [n], as defined in §2.14.5/8:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

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:

It is unspecified whether these arrays are distinct provided their elements have the appropriate values.

while in C++ this is implementation defined, as per §2.14.5/13:

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation- defined.

like image 127
Shoe Avatar answered Oct 03 '22 22:10

Shoe