printf的忽略一个反斜杠“\\” [英] printf ignores single backslash '\'
问题描述
我有这样的code:
int main(int argc, char * argv[])
{
int i;
printf("%d%s",argc,argv[1]);
return 0;
}
如果我运行这个code作为文件a.out \\ = B =
。我现在用的C-shell
If I run this code as a.out a\=b=
.I am using C-shell
它的输出是 A = B =
有什么办法,它的输出可以改为 A \\ = B =
Its output is "a=b=
" is there any way that its output can be changed to "a\=b=
".
推荐答案
更新编辑问题
您括命令行参数放在引号:
Enclose your command line argument in quotes:
$ a.out "a\=b="
引号prevent从国际preting以任何方式命令行参数的外壳,让眼前这个字符串传递给你的程序。我用的是 CSH
/ 庆典
..works两者兼具的。
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答案,原来的问题
是,使用两个 \\
:
char a[]="a\\=b=";
输出:
a\=b=
说明
\\
是用来表示一种特殊的字符序列转义字符,所以例如 \\ t
表示制表。如果你想实际打印 \\ t
,你需要逃离这个 \\
另一个 \\
。见这个例子和输出:
\
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 \\ */
这会导致:
-->Hi
\t-->Hi
这是的不的独有的的printf()
功能,也确实给C,可能语言使用反斜杠表示转义序列在字符串中。
This is not unique to the printf()
function, nor really to C, may languages use the backslash to indicate "escape sequences" in strings.
这篇关于printf的忽略一个反斜杠“\\”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!