2

I had to made something for a little programm some days ago, I needed to turn a string like "foo\n" into its stringified form (something like "\"foo\\n\"").

But I found nothing, so I ended up writing my own function (and it worked fine). But I'm still wondering : Is there a function to stringify strings like this?

I know the python equivalent "%r" % strtostringify, and I know that I can strinfifying pieces of code at compiling with preprocessor directives :

#define TOSTRING(X) #X
#define STRINGIFY(X) TOSTRING(X)

but is there a way to make it dynamically in C?

vmonteco
  • 14,136
  • 15
  • 55
  • 86
  • 1
    The process you're describing is normally called "escaping" , and there's no built-in escaping function in C (nor in the preprocessor). "stringify" means to change `X` into `"X"`, where `X` is a pre-processing token (not an arbitrary sequence of bytes) – M.M May 10 '17 at 20:39
  • @M.M Actually, the `gcc` documentation's refers to the `#` use in preprocessor directives as "stringizing" (perhaps both words are used?), but thank you for making it clear that there is no such thing in C standard library. :) https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing – vmonteco May 10 '17 at 20:47
  • @AppWriter Done, thank you for the suggestion! – vmonteco May 10 '17 at 20:53
  • yes, the `#` operator is for stringizing as I described in my comment (not escaping) – M.M May 10 '17 at 21:23

1 Answers1

4

Escaping special characters is not provided in any standard function.


For GPL, you can take a look in standard c library for escaping a string.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305