printf ignores single backslash '\'

23,713

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: \\.

Share:
23,713
nav_jan
Author by

nav_jan

Updated on July 17, 2022

Comments

  • nav_jan
    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 using C-shell

    Its output is "a=b=" is there any way that its output can be changed to "a\=b=".

  • nav_jan
    nav_jan almost 12 years
    how can that be done? can i write a code that can convert "a\=b=" to "a\\=b="
  • nav_jan
    nav_jan almost 12 years
    how can that be done? can i write a code that can convert "a\=b=" to "a\\=b="
  • nav_jan
    nav_jan almost 12 years
    how can that be done? can i write a code that can convert "a\=b=" to "a\\=b="
  • Juri Robl
    Juri Robl almost 12 years
    @nav_jan No, just type \\ if you want to display \ .
  • Levon
    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
    FatalError almost 12 years
    As in you have it many places and want to change them? Sounds like a job for search+replace or a regex.
  • nav_jan
    nav_jan almost 12 years
    no i am giving the string from command line a.out a\=b= and i want to print argv[1].
  • nav_jan
    nav_jan almost 12 years
    what if when i am giving the string from command line a.out a\=b= and i want to print argv[1]
  • FatalError
    FatalError almost 12 years
    A 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
    FatalError almost 12 years
    c-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.