printf("%s", str) vs. printf(str)
时间:2008-05-17 来源:剑心通明
printf("%s", str) vs. printf(str)
Created: Fang lungang 03-08-2007Modified: Fang lungang 03-08-2007 21:35>
从 The C Programming Language 2nd 看到的 printf 一个需要注意的细节:即使被打印的本身就只有一个字符串,最好也别直接打印,而要用格式符 %s。其它格式输出的函数应该也类似。
#include
int main()
{
char* str= "a string with % may not workn";
printf ("%s", str);
printf (str);
return 0;
}
[/home/lungangfang/tmp]gcc --version
gcc (GCC) 3.2.3 20030502 (Red Hat
[url=javascript:;]Linux[/url]
3.2.3-20)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[/home/lungangfang/tmp]./a.out
a string with % may not work
a string with Successay not work
[/home/lungangfang/tmp]
Created: Fang lungang 03-08-2007Modified: Fang lungang 03-08-2007 21:35>
从 The C Programming Language 2nd 看到的 printf 一个需要注意的细节:即使被打印的本身就只有一个字符串,最好也别直接打印,而要用格式符 %s。其它格式输出的函数应该也类似。
#include
int main()
{
char* str= "a string with % may not workn";
printf ("%s", str);
printf (str);
return 0;
}
[/home/lungangfang/tmp]gcc --version
gcc (GCC) 3.2.3 20030502 (Red Hat
[url=javascript:;]Linux[/url]
3.2.3-20)
Copyright (C) 2002 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[/home/lungangfang/tmp]./a.out
a string with % may not work
a string with Successay not work
[/home/lungangfang/tmp]
相关阅读 更多 +