Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparison between the two printf statements

Tags:

c

string

printf

please take a look at the two following c statements

printf("a very long string");
printf("%s","a very long string");

they produce the same result,but there is definitely some difference under the hood,so what is the difference and which one is better? Please share your ideas!

like image 550
Tracy Avatar asked Jul 19 '26 08:07

Tracy


1 Answers

If you know what the string contents are, you should use the first form because it is more compact. If the string you want to print can come from the user or from any other source such that you do not know what the string contents are, you must use the second form; otherwise, your code will be wide open to format string injection attacks.

like image 193
Aasmund Eldhuset Avatar answered Jul 20 '26 23:07

Aasmund Eldhuset



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!