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

4

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

Continuum answered 21/4, 2016 at 6:44 Comment(0)
C
6

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 \’

Cote answered 21/4, 2016 at 6:58 Comment(0)
R
4

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 \".

Reviere answered 21/4, 2016 at 6:48 Comment(0)
D
2

\' 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++.

Deprecative answered 21/4, 2016 at 6:49 Comment(0)
H
2

Yes, they're identical.

From the c++ standard, $2.13.3/7 Character literals [lex.ccon]

Table 6 — Escape sequences

new-line         NL(LF) \n
horizontal tab   HT     \t
vertical tab     VT     \v
backspace        BS     \b
carriage return  CR     \r
form feed        FF     \f
alert            BEL    \a
backslash        \      \\
question mark    ?      \?
single quote     ’      \’
double quote     "      \"
octal number     ooo    \ooo
hex number       hhh    \xhhh
Hyperbole answered 21/4, 2016 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.