Risk of printf(str)
08 March 2007
The C Programming Language 2nd mentioned a subtle issue of printf
: even if
there is only one literal string to printf, do print it with format specifier
"%s". The rule applies other similar print functions as well. The reason? Read
on.
#include <stdio.h> int main() { char* str= "a string with % may not work\n"; printf ("%s", str); printf (str); return 0; }
[/home/lungangfang/tmp]./a.out a string with % may not work a string with Successay not work [/home/lungangfang/tmp]gcc --version gcc (GCC) 3.2.3 20030502 (Red Hat Linux 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.
blog comments powered by Disqus