Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "'" identical to "\'" as per the C/C++ standard?

int main()
{
    char* str1 = "Tom's cat";
    char* str2 = "Tom\'s cat";
}

The code can be compiled with VS 2015.

I just wonder:

Are both of the two ways compliant to the C and/or the C++ standard?

like image 546
xmllmx Avatar asked Apr 21 '16 06:04

xmllmx


3 Answers

From the C++11 ISO Standard

§ 2.14.5 String Literals [lex.string]

...

15 Escape sequences and universal-character-names in non-raw string literals have the same meaning as in character literals (2.14.3), except that the single quote ’ is representable either by itself or by the escape sequence \’

like image 81
Galik Avatar answered Oct 02 '22 05:10

Galik


Yes, within a string literal, both are the same.

The escaped version is required for a character literal:

char x = '\'';

The standard references are two sources. First, translation phases. From C.11 §5.1.1.2 (C++.11 [lex.phases] has similar language):

  1. Each source character set member and escape sequence in character constants and string literals is converted to the corresponding member of the execution character set; if there is no corresponding member, it is converted to an implementation defined member other than the null (wide) character.

Next is in the grammar definition for a character constant and for string literals, which allow for escape sequences. And simple-escape-sequence is an escape sequence in the grammar. C.11 §6.4.4.4 defines it (C++.11 [lex.ccon] has the same definition):

simple-escape-sequence: one of

\' \" \? \\
\a \b \f \n \r \t \v

Finally, for string literals, the standard specifies the interpretation of characters in the literal is the same as if each were a character constant, and then makes an exception of '. From C.11 §6.4.5 (C++.11 [lex.string] has similar language):

The same considerations apply to each element of the sequence in a string literal as if it were in an integer character constant (for a character or UTF−8 string literal) or a wide character constant (for a wide string literal), except that the single-quote ' is representable either by itself or by the escape sequence \', but the double-quote " shall be represented by the escape sequence \".

like image 37
jxh Avatar answered Oct 02 '22 04:10

jxh


\' is a valid character escape sequence in both C and C++. Hence, the lines

char* str1 = "Tom's cat";
char* str2 = "Tom\'s cat";

produce equivalent string literals, both in C and C++.

like image 33
R Sahu Avatar answered Oct 02 '22 05:10

R Sahu