printf and puts


3 replies [Last post]
raunaqrox
raunaqrox's picture
Offline
Joined: 2012-06-24
Points: 0

Newbie here . 

Whats the difference between 'printf' and 'puts' . Isnt it better to use 'puts' for basic printing of characters as one would'nt have to type \n .

 

 

I love the videos here :)

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Bryan
Offline
When things break I did it. I am an admin!Enjoy my soothing baritone.Completionist. I am better than you.My name if Forest WiBit.Nothing on Earth could stop the coding...A Coding BeautyDriving Ms. ChickyYou maniac! You blew it up! The compiler that is.Halfsies!W007! I watched 10 vids!
Joined: 2011-04-22
Points: 2310

"puts" is definitely quick and convenient! The big difference is that printf has a format string option.

You can also argue that puts is a little safer. If for example, you pass user input into a "printf" or an "sprintf" function on accident such as:

printf(userinput);

Instead of:

printf("%s", userinput);

The user can maliciously format the input string and hack your application (exploit a security vulnerability).

Here's a nice wiki page on string vulnerabilities:
http://en.wikipedia.org/wiki/Format%5Fstring%5Fvulnerabilities

Bryan

raunaqrox
raunaqrox's picture
Offline
Joined: 2012-06-24
Points: 0

Ok , got it . Thank you .

Trevor266
Trevor266's picture
Offline
Joined: 2013-01-27
Points: 0

puts also appends a newline to the end of the statement