int main(int argc, char **argv) {
char *str;
str = argv[1];
printf("%s", str);
return 0; }当程序使用这两个字符串执行时:
$./program "\abc"
$./program "\\abc"两个run都在str变量中存储相同的字符串,如str = \\abc。
使用\\abc运行时如何获得四个反斜杠,与\abc一起运行时如何获得单个反斜杠?
发布于 2019-02-02 11:16:53
您将永远不会从\\获得四个反斜杠,但是如果您想从字面上传递字符串,请使用单引号
单引号(
')中包含的字符保留引号中每个字符的文字值。单引号之间可能不会出现单引号,即使在前面加上反斜杠时也是如此。
例如:
./program '\abc' # passes a four-character string: \ a b c
./program '\\abc' # passes a five-character string: \ \ a b c发布于 2019-02-02 11:17:01
我猜你在你的控制台里使用了Bash。您的问题与C无关,而不是Bash进程字符串。根据man
双引号中包含的字符保留引号中所有字符的文字值,除$外,
, \, 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在双引号中保留其特殊意义。反斜杠只有在后面跟着下列字符之一时才保留其特殊意义:$、`、“、\或换行符。双引号可以在双引号中以反斜杠形式引用。如果启用了历史扩展,则将执行历史扩展,除非使用反斜杠转义出现在双引号中的a!。
因此,在Bash中,"\a"和"\\a"都被视为“a”。如果你想在双引号中有四个反斜杠,你需要写其中的8个。试试echo "\\\\\\\\"
https://stackoverflow.com/questions/54492324
复制相似问题