Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C String Literal Required Escape Characters

Tags:

c

string

escaping

I have a zero terminated string:

char* s = ...;

and I am generating C source code (at runtime) and I want to output a string literal representing s that will produce an identical string to s in the generated C program.

The algorithm I am using is:

Output "

Foreach char c in s
    if c == " output \"
    else if c == \ output \\
    else output c

Output "

Are there any other characters that I need to give special treatment other than " and \?

like image 817
Andrew Tomazos Avatar asked Dec 16 '22 20:12

Andrew Tomazos


1 Answers

  • You must encode ", \, \r and \n and \0 (and \? as Michael Burr mentions). Failure to do this will break your code.
  • You should encode non-ASCII characters using the hexadecimal escape code, e.g. \x80. It is implementation defined if you have non-ASCII characters in your source code. Failure to encode these characters will work on some compilers but it could break on others.
  • You can encode ASCII non-printable characters. It would improve the readability of the generated source code if you used the escape codes for characters like \t, \b, \x05, etc. If you don't do this your code will still work but it might be hard to read.
  • You don't need to escape ' inside a double-quoted string. It's legal, but it's unnecessary and it doesn't make the source code more readable.
like image 92
Mark Byers Avatar answered Dec 30 '22 01:12

Mark Byers