PSkocik PSkocik - 5 days ago 6
C Question

Escaped regular characters

Are strings such as

"\$"
illegal? Why or why not? (Gcc and clang give a warning but treat it as if it was
"$"
) How is
\
followed by a character that, with the backslash prepended, doesn't form a reserved escape sequence supposed to behave?

Answer

It is explicit in a (non normative) note of the draft n1570 for C11. The 6.4.4.4 Character constants paragraph defines escape sequences as:

escape-sequence:

  • simple-escape-sequence
  • octal-escape-sequence
  • hexadecimal-escape-sequence
  • universal-character-name

simple-escape-sequence: one of

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

octal-escape-sequence:

  • \ octal-digit
  • \ octal-digit octal-digit
  • \ octal-digit octal-digit octal-digit

hexadecimal-escape-sequence:

  • \x hexadecimal-digit
  • hexadecimal-escape-sequence hexadecimal-digit

All other sequences of another character following a \ are not defined here, so the behaviour is unspecified (not undefined) by current standard

A note says:

77) ... If any other character follows a backslash, the result is not a token and a diagnostic is required. See ‘‘future language directions’’ (6.11.4).

And 6.11.4 says:

6.11.4 Character escape sequences

Lowercase letters as escape sequences are reserved for future standardization. Other characters may be used in extensions.

Commonly, compilers issue the required warning but just ignore the superfluous \. It is fully conformant for non lowercase letters as it can be a local extension, but it could break in a future version of the C language for lower case letters because it is explicitely a reserved feature

Comments