1
int main(int argc, char **argv) { char *str; str = argv[1]; printf("%s", str); return 0; } 

When program is executed using these two strings:

$./program "\abc" $./program "\\abc" 

Both run stores same string in str variable, as str = \\abc.

How to get four backslashes when run with \\abc or single backslash when run with \abc ?

5
  • Which terminal do you use to execute your program? Commented Feb 2, 2019 at 10:53
  • I am using default ubuntu terminal Commented Feb 2, 2019 at 10:55
  • 2
    This has nothing to do with C and everything to do with how you run your program (i.e. your shell), which apparently is bash. Commented Feb 2, 2019 at 11:07
  • I can't reproduce the problem. I get \abc as output, not \\vabc. Commented Feb 2, 2019 at 11:11
  • 1
    You don't need a C program to expose the argv array. Try something like printf '>>%s<<\n' "$@" at your shell prompt (or put it in a script file with a suitable shebang). Commented Feb 2, 2019 at 11:27

2 Answers 2

2

You will never get four backslashes from \\, but if you want to pass a string literally, use single quotes:

Enclosing characters in single quotes (') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.

For example:

./program '\abc' # passes a four-character string: \ a b c ./program '\\abc' # passes a five-character string: \ \ a b c 
Sign up to request clarification or add additional context in comments.

7 Comments

is there any way to detect if single backslash or double backslash passed in the string (between double quotes).
@Jaat What are you talking about?
is there any way to retain the literal value of single backslash while using double quotes to pass the string?
@Jaat Use a shell that is not bash (and not compatible with any other unix shell)? Why do you want to redefine how the shell works?
i am trying to make a program that dumps hex of a string. But the problem is that "\\n" resulted into '0a' instead of '5c6e'.
|
1

I assume you are using Bash in your console. Your issue is not related to C, rather than how does Bash process strings. According to man:

Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $, `, \, and, when history expansion is enabled, !. When the shell is in posix mode, the ! has no special meaning within double quotes, even when history expansion is enabled. The characters $ and ` retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or newline. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.

So in Bash both "\a" and "\\a" are treated the same as "\a". If you want to have four backslashes in double quotes you will need to write 8 of them. Try echo "\\\\\\\\"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.