c - How to escape a character -


i have situation need escape character. trying form buffer

char buffer[1024];  char* x="hello"; char* y="world"; sprintf(buffer,"=%s\n-%s\n ",x,y); 

my goal here buffer such as

=hello\n-world\n 

in exact order. signs in between such (=, \n, -) specific action when buffer passed function.

however, if present code printed shown in above code get

=hello -world 

instead of

=hello\n-world\n 

but not desired. how can achieve desired result?

you need escape backslash, : \\

sprintf(buffer,"=%s\\n-%s\\n ",x,y); 

Comments