Code such as printf(foo); often indicates a bug, since foo may contain a % character. If foo comes from untrusted
user input, it may contain %n, causing the printf() call to write to memory and creating a security hole.
所以如果printf(str)中如果有%n,则会把已经输出的字符串的个数保存到一个变量中,如果没有指定此变量,则会保存到一个不确定的地址。如下例:
#include
int main(int argc, char *argv[])
{
int i = 10, n = 0;
char * str = "This is test:i=%d, %n\n";
printf("argv[0] = %s\n", argv[0]);
if(argc > 1)
printf("Hello world! I am a %s\n", argv[1]);
printf(argv[0]);
printf("\n");
printf(str);
printf("This is test:i=%d,%n", i, &n);
printf("n = %d\n", n);
return 0;
}