Tra*_*acy 2 c compiler-construction string printf
请看下面两个c语句
printf("a very long string");
printf("%s","a very long string");
Run Code Online (Sandbox Code Playgroud)
它们会产生相同的结果,但在引擎盖下肯定存在一些差异,那么差异哪个更好?请分享您的想法!
Aas*_*set 13
如果你知道字符串内容是什么,你应该使用第一个表单,因为它更紧凑.如果要打印的字符串可以来自用户或任何其他来源,以至于您不知道字符串内容是什么,则必须使用第二种形式; 否则,您的代码将广泛开放以格式化字符串注入攻击.
第一个printf像这样工作
'a' is not a special character: print it
' ' is not a special character: print it
'v' is not a special character: print it
...
'g' is not a special character: print it
Run Code Online (Sandbox Code Playgroud)
第二个printf是这样的
'%' is a special character:
's' print the contents of the string pointed to by the 2nd parameter
Run Code Online (Sandbox Code Playgroud)