Reputation: 33
I wanted to print \0
using printf
. First I found printf("ab\0c")
is not right because I can only get "ab
". Then I found a way to print "ab\0c
" using printf("ab\\0c")
. But that brings a new problem. What if I want to print "ab\\0c"
? I want to print it in only one sentence. Do you have any good way to do this?
Upvotes: 2
Views: 5059
Reputation: 29266
\
"escapes" special characters (allows you to write \n
and \"
etc). If you want to print a \
you need to escape that (since \
is a special character itself). Put \\
in your string to print \
.
If you want to print \\
your format string has to be \\\\
(i.e. escape both \
s).
Upvotes: 3
Reputation: 65264
Printing a single backslash (as opposed to using it as an escape sequence) needs a double backslash to undo the escaping., so basically
ab\0c
you need printf("ab\\0c")
(2 times 1 is 2)ab\\0c
you need printf("ab\\\\0c")
(2 times 2 is 4)Upvotes: 7