printf ignores single backslash '\'
Solution 1
Update for edited question:
Enclose your command line argument in quotes:
$ a.out "a\=b="
The quotes prevent the shell from interpreting the command line argument in any way, so just this string is passed to your program. I use the csh
/bash
..works with both.
Alternatively, you can "escape" the \
with another one and skip the double quotes:
$ a.out a\\=b=
Previous answer to original question:
Yes, use two \
:
char a[]="a\\=b=";
outputs:
a\=b=
Explanation:
\
is an escape character used to indicate a special character sequence, so for instance \t
indicates a tab. If you want to actually print \t
, you need to "escape" this \
with another \
. See this example and output:
printf("\t-->Hi\n"); /* print regular tab via \t */
printf("\\t-->Hi\n"); /* want to print "\t", not tab ..so we use two \\ */
which results in:
-->Hi
\t-->Hi
This is not unique to the printf()
function, nor really to C, may languages use the backslash to indicate "escape sequences" in strings.
Solution 2
printf()
is not ignoring your single backslash, it's the way C strings are parsed. A backslash is an escape character to indicate some character that is not easily entered in a string, for example a newline (\n
) or embedded quote (\"
). Consequently, to include a backslash you must include two backslashes (\\
). This is for all strings, and not related to printf()
.
Solution 3
The \
is the escape character in strings in C. You use it to access special character such as newline (\n
). If you want to access the \
as a character, you need too escape it: \\
.
nav_jan
Updated on July 17, 2022Comments
-
nav_jan almost 2 years
I have this code :
int main(int argc, char * argv[]) { int i; printf("%d%s",argc,argv[1]); return 0; }
If I run this code as
a.out a\=b=
.I am usingC-shell
Its output is "
a=b=
" is there any way that its output can be changed to "a\=b=
". -
nav_jan almost 12 yearshow can that be done? can i write a code that can convert
"a\=b="
to"a\\=b="
-
nav_jan almost 12 yearshow can that be done? can i write a code that can convert
"a\=b="
to"a\\=b="
-
nav_jan almost 12 yearshow can that be done? can i write a code that can convert
"a\=b="
to"a\\=b="
-
Juri Robl almost 12 years@nav_jan No, just type
\\
if you want to display\
. -
Levon almost 12 years@nav_jan Not sure I understand your question -- even though your put two back-slashes, printf will interpret it correctly and generate one on output.
-
FatalError almost 12 yearsAs in you have it many places and want to change them? Sounds like a job for search+replace or a regex.
-
nav_jan almost 12 yearsno i am giving the string from command line
a.out a\=b=
and i want to print argv[1]. -
nav_jan almost 12 yearswhat if when i am giving the string from command line
a.out a\=b=
and i want to print argv[1] -
FatalError almost 12 yearsA string you enter from your shell won't be interpreted by C in this way it all. However, it may be interpreted by your shell. To know for sure, we'll need to know which shell you're using, though.
-
FatalError almost 12 yearsc-shell will indeed interpret the backslash away unless you quote or escape it, any one of
a\\=b
or"a\=b"
or'a\=b'
ought to work.