Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the backslash-percent escape in C?

Tags:

c

gcc

This compiles without warning in clang and gcc:

const char *foo = "\%";

The resulting string is the same as "%".

What is this escape for? Where can I find a complete list of escapes?

I thought maybe it was for escaping digraphs, but other digraph characters produce warnings (e.g. "\:").

Thanks for any help!

like image 374
ridiculous_fish Avatar asked Feb 08 '23 08:02

ridiculous_fish


2 Answers

Answering my own question: it's to support SCCS, which is a 40-year old revision control system, that predates even RCS. Ha!

Digging into the compilers, clang supports this because it thinks gcc does:

case '(': case '{': case '[': case '%':
// GCC accepts these as extensions.  We warn about them as such though.

Lies! The warnings show up, but only with the -pedantic flag (in both compilers).

Oh, and gcc? It supports the first three for emacs, which I guess is easily confused:

/* `\(', etc, are used at beginning of line to avoid confusing Emacs.  */
case '(':
case '{':
case '[':

but the last one:

  /* `\%' is used to prevent SCCS from getting confused.  */
case '%':
  if (pedantic)
      pedwarn ("non-ANSI escape sequence `\\%c'", c);
  return c;

SCCS support! It's glorious!

like image 71
ridiculous_fish Avatar answered Feb 13 '23 04:02

ridiculous_fish


According to the C standard, any escape sequence not mentioned by the Standard is a syntax error.

This means that the compiler must produce a diagnostic, but the compiler could define an extension (e.g. ignore the \) and continue compiling the rest of the program.

The standard escape sequences are: \' \" \? \\ \a \b \f \n \r \t \v, and also the octal, hex and universal character constants introduces by \(digit), \x and \u respectively.

like image 43
M.M Avatar answered Feb 13 '23 04:02

M.M