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 \?
", \, \r and \n and \0 (and \? as Michael Burr mentions). Failure to do this will break your code.\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.\t, \b, \x05, etc. If you don't do this your code will still work but it might be hard to read.' inside a double-quoted string. It's legal, but it's unnecessary and it doesn't make the source code more readable.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With